SetAsync doesn't work..?

local DSS = game:GetService(“DataStoreService”)
local DS = DSS:GetDataStore(“Stats”)
function saveData(key, data)
local success, errormsg = pcall(function()
DS:SetAsync(key, data)
end)
if not success then
warn(errormsg)
else
print(“saved”)
end
end
saveData(plr.UserId…“money”, 6)
print(“Done”)

This script is not working. It’s wrapped in a PlayerRemoving function.
When I try this, “Done” doesn’t print and the output is completely empty. Does anyone know what’s going on? I’ve been trying to fix this for hours.
(adding a print statement before running the function works.)

1 Like

I don’t recommend using functions for saving datas (Because you won’t use it a lot), as game might mistake plr.UserId…“Money” to something else then a stringvalue. Did you try this without using a function?

I tried it without a function and it didn’t work either. Nothing prints after the GetAsync().

1 Like

did you try storing plr.UserId…“money” to a variable?

If you’re testing this in Studio, make sure you have the option turned on in your game for Studio to use API services.

What’s your testing method? Play Solo for example closes the player and server at the same time so you probably would never get to the result of the operation.

Test Server with a player that you remove whilst keeping the server alive is the best way I’ve found to test PlayerRemoving behaviour.

It’s also pretty hard to test in a live game without using friends to join and leave whilst you’re waiting in-game.

2 Likes

I tried storing it in a variable and it didn’t work.

I’m playing it in solo. I’ll try playing in in a server.