Question about :UpdateAsync() when it throws an error

When :UpdateAsync() throws an error (Due to roblox) does it still execute all code within the function?

local bool = false

Datastore:UpdateAsync(Key, function(PastData)
    bool = true
    return PastData
end)

Will bool be true if the function throws an error due to roblox servers? I know that there are different errors that can occour, will some of them still change bool?

local bool = false

Datastore:UpdateAsync(Key, function(PastData)
    bool = true
    workspace.Part1.Position = vector3.zero
    return PastData
end)

In this example, if there is no Part1 an error will be thrown. Will it still keep the value of bool to equal true or how does it work?

Yes, code execution will only halt on the line an error occurs. So the variable will still be equal to true even if the function errors later on.

But don’t forget that functions don’t run in their own environment, if that function errors your entire script will halt execution. You should wrap this code in a pcall() to make sure the script will keep working even if an error occurs.

local bool = false

local success, errorMessage = pcall(function()
   DataStore:UpdateAsync(key, function()
      bool = true
   end)
end)

if success then
   -- if the function executed without errors, do stuff
   print(bool)
else 
   warn(errorMessage)
end
1 Like