Well, pcall
stands for protective call or something. It’s used to prevent errors from going out into the console and stopping the current script. It returns 2 things:
- A bool indicating if the block of code in the call ran successfully. (true if successful, false if not and errored)
- An error message that came from the block of code if it didn’t run successfully and errored.
Usage:
local SUCCESS, ERROR = pcall(function()
--your block of code you want to "protect"
end)
if (SUCCESS) then
--if the call was executed successfully
else
--the call must have not been successful so there must be an error
warn(ERROR) -- logs the error into the console. That error tells you why the call failed.
end
OH YES.
P.S - You can return values to the call
local SUCCESS, OTHER = pcall(function()
--your block of code you want to "protect"
return "hi"
end)
if (SUCCESS) then
--if the call was executed successfully
print(OTHER) -- prints "hi" because you returned "hi" to the variable
else
--the call must have not been successful so there must be an error
warn(OTHER) -- logs the error into the console. That error tells you why the call failed.
-- When it fails, "OTHER" becomes the error
end
Now onto the Datastore.
I see you did this, :GetAsync()
returns the data you saved when the player left. So, you should either return the data to the call or define a variable outside and set the data to that.
Example:
--Variable
local Data
--pcall blah
Data = Datastore:GetAsync(ID)
--If the call was successful, you have the data you saved in the variable called "Data"
--OR using the returned method
local success, data = pcall(function()
return Datastore:GetAsync(ID)
end)
if (success) then
--poof, data is now the data you saved
print(data)
else
--ouch, data is an error now, we need to print it to find out why the call failed
warn(data)
end
Some information might be wrong, if there’s any misleading info, please tell me so I can fix it.
Sorry I couldn’t go to :SetAsync()
. I’m currently in a rush. I’ll come back to tell you about it if someone else doesn’t.
And yep, use the parameter from PlayerAdded.
I USED CONSOLE, IT’S MEANT TO BE OUTPUT.