#Pcall Question

1 messages · Page 1 of 1 (latest)

stoic nimbus
#

Hi! I know what a pcall does, but I'm kinda stuck on how to use it in an actual game. I know it creates a protected function, but when is this necessary (with examples, preferred)? I would love some feedback!

balmy night
#
local function someError()
    error("Im not working")
end

local function noError()
    local something = 2
    return something
end

local success, result = pcall(someError)
local success, result = pcall(noError)

so in one function theres an error, success is aboolean (true or false) and result is either the error message or the actual return from the function.

If you would print result in the first function it would print the error ("Im not working" and in the other function it would print the variable something.

#

@stoic nimbus

crisp charm
shy jasper
#

pcall is used for protecting the call obviously

#

ok but other than that

#

when people do local success, result = pcall(function()

#

success would be a boolean

#

i mean

#

result

#

forgot which one

balmy night
crisp charm
#

its just the way i have been using them until now

#

but which other secure way could you use to save data without a pcall?

shy jasper
#

profileservice

#

the actual module profileservice prob has it

balmy night
shy jasper
#

not when you have to like actually make the script

shy jasper
#

6 pcalls

crisp charm
shy jasper
#

impossible? idk

#

just remove the pcall part and see what happens

#

frfr

crisp charm
#

💔

balmy night
#

you don't necessarily need pcalls they just make it "safer"

crisp charm
#

ig.