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

what does Pcalling SetAsync return?

Asked by 4 years ago

So if GetAsync returns if it was true or false on the first parameter and second one it returns the value returned

local S,E = pcall(function()
    local DataStoreGot = DataStoreOfAllStatsAndCurrency:GetAsync(Player.UserId)
    return DataStoreGot
end)

S would be true or false or nil

E would return the DataGot from GetAsync

but what I don't understand is what does SetAsync return

local S,E = pcall(function()
        local DataStoreSave = DataStoreOfAllStatsAndCurrency:SetAsync(Player.UserId,DataStoreSpaceModule[Player.UserId])

        return DataStoreSave

    end)

i tried to print(E) the second parameter but nothing occurred so I was wondering what S and E returns

1 answer

Log in to vote
4
Answered by 4 years ago
Edited 4 years ago

pcall returns a tuple of two values. The first value is whether or not the provided function ran without errors. The second value is changeable based on the situation. If the function errored, it is the error message. If the function did not error, it is the return value(s) of the function provided. pcall means "protected call," which makes sense, if you think about it. Here's are a few examples:

local a, b = pcall(function()
    return 5
end)
print(a, b) --> true    5

local a, b = pcall(function()
    return 5 + "x"
end)
print(a, b) --> false   somethinghere: attempt to perform arithmetic on a string value

local a, b = pcall(function()
end)
print(a, b) --> true    nil

In your case, SetAsync likely doesn't return anything. Since you're returning nothing, that nothing is being printed. I believe it should be printing nil, since that's what is outputted when I do this:

local a, b = pcall(function()
    return (function() end)()
end)
print(a, b) --> true    nil

In case you didn't know, this outputs nothing (not nil, nothing):

print((function() end)()) -->

If you are even more interested, you can look into what happens when you do something like this:

print(type((function() end)())) --> somethinghere: bad argument #1 to 'type' (value expected)

For comparison, take a look at type with nil as the argument:

print(type(nil)) --> nil

After further experimentation, I found that, if a variable is defined as nothing, it is nil (value changed at assigning?), which was surprising to me:

local x = (function() end)()
print(x) --> nil

That means that your E should be nil, even if SetAsync returns nothing (which it does, I just looked at the wiki). For your information, even in the case of the function returning nothing, pcall returns that value if the code within ran successfully:

print(select(2, pcall(function() end))) -->

So... not sure what's going on in your case (perhaps some weird behavior on Roblox's end). Cause E should be nil by the behavior demonstrated my my examples, even if pcall is returning the tuple true,.


Hope this helps. Have a great day scripting!

Ad

Answer this question