I used to use update async and recently I started developing a new game and I'm having trouble with the update async. These are the variables
1 | local gui = game.ServerStorage.Overhead --ignore its useless |
2 | local ds = game:GetService( "DataStoreService" ) |
3 | local skill = ds:GetDataStore( "Levels" ) |
4 | local key = "-level" |
Then this is the part of the script that doesn't work
01 | game.Players.PlayerRemoving:Connect( function (pla) |
02 | local suc, err = pcall ( function () |
03 | print ( 'hi' ) --prints |
04 | skill:UpdateAsync( tostring (pla.UserId..key), function (oldval) |
05 | print ( 'update' ) --doesn't print |
06 | if tonumber (oldval) = = nil then print ( "it is nil and saved" ) return pla.leaderstats.Level.Value end |
07 | if pla.leaderstats.Level.Value > = oldval then |
08 | print ( 'returned more' ) |
09 | return pla.leaderstats.Level.Value |
10 | else |
11 | print ( 'less than save' ) |
12 | return oldval |
13 |
14 | end |
15 | end ) |
16 | end ) |
17 | end ) |
NO ERRORS
You are calling UpdateAsync inside of a pcall. pcall is short for protected call, essentially meaning any error that kicks will not cause Lua to error, but instead be returned as strings. This is to let you handle those errors within Lua.
01 | local status, errorString = pcall ( function () |
02 | return datastore:SetAsync(key, value) |
03 | end ) |
04 |
05 | if status = = false then -- this boolean will be false if an error happened in the pcall |
06 | warn(errorString) -- warn the error to output so you can read it without halting execution |
07 |
08 | else -- otherwise it all worked out without erroring, do what you need to here |
09 |
10 | end |