How to tell when datastores fail?

Recently I was testing my game, and the datastores just happened to fail. They gave me a 500 error code, and then my GetAsync() request dropped. Anyways, my datastores use UpdateAsync(),so I thought this would not be a problem as it would just not save the users play session. However, when the GetAsync() request failed it set my collectibles, coins, etc. all to 0. Then, when I left the game saved all of my stats as 0. So, I am basically looking for a way to tell if a GetAsync request fails. If it does, I would like to kick the player WITHOUT saving their data. I know how to kick them/not save the data, I am just lost on how to tell if my GetAsync() request fails.

I would appreciate any help. Thanks in advance!

2 Likes

maybe this?

local s = pcall(function() -- s == success
--code
end)
1 Like

I have wrapped it in a pcall, I am not 100% certain on the way that success works though. Is success true until an error occurs, or is it false until the pcall finishes?

I know that the “success” value is false when an error occurs.
If error doesn’t occur, then it’s true.

1 Like
s = pcall(function() 
print(nil+2) 
end) 

print(s) -- false

error: attempt to perform arithmetic (add) on nil and number

1 Like

pcall runs until an error is occurred, at which point it will break out, returning false and the error

local x = 0

pcall(function()
  x += 1
  error()
  x += 1
end)

print(x) --> 1

Roblox will handle critical DataStore errors for you, remember, if GetAsync throws an error, its likely something went wrong on Roblox’s end.

2 Likes

Dude it’s easy. Pcall has handled it for you.

local success,errmsg = pcall(function()
GetAsync(data) -- ur GetAsync code here
end)

if not success then
warn("big chungus alert, Roblox trashy datastore failed!")
end
1 Like