Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What does SetAsync second parameter return in pcall?

Asked by 5 years ago

So If

local S,E = pcall(function()
        return DataStore:GetAsync('DataKey') end)

S returns true or false and E returns the Data that was saved

But I noticed that for SetAsync its different

local S,E = pcall(function()
        return SetAsync('DataKey',Value) end)

E dosent return anything it returns nil does anyone know what this E is meant to be for

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I assume you know how pcalls work but it never hurts to go through it again:

When the pcall works but it's body has errored


local a, b, c = pcall(function()
    return 1 - "three", 20
end)

print(a, b, c) --> false    input:2: attempt to perform arithmetic on a string value nil

So in case of an error in the body we can observe that: 1. The first variable - a - becomes false, meaning success of the pcall was false

  1. The second variable - b - becomes a string with the error that occured in the pcalls body

  2. All other variables which would normally get a returned value were nil, no values have been returned

When the pcalls body runs safely


local a, b, c = pcall(function()
    return 1 *3, 1 + 3
end)

print(a, b, c) --> true 3 4

This time we can see that:

  1. The first variable shows the body ran successfully

  2. b- which was previously the error string - is now the first returned value and c is the second

Your problem


Your problem is that

return datastore:SetAsync(...)

Is as good as doing

return nil

Why? It's since the datastore:SetAsync() method returns no value at all. Hence the variable E which is the value returned by the body, is nil.

Ad

Answer this question