The overall goal of this is that I am making a Halloween update for my game, and the amount of candy a player owns will be saved. API Services are enabled, but the :SetAsync() function wouldn't work. The script will print "Saving..." but never says saved afterward; thus meaning the data didn't save.
Here is the script:
local year = tonumber(math.floor(os.time()/60/60/24/365.25+1970))
local dss = game:GetService("DataStoreService")
local candySave = dss:GetDataStore("HalloweenCandy-".. tostring(year))
local broomSave = dss:GetDataStore("HalloweenBroom-".. tostring(year))
game.Players.PlayerRemoving:Connect(function(plr)
if plr:FindFirstChild("HalloweenData") then
local hd = plr:FindFirstChild("HalloweenData")
if plr:FindFirstChild("HalloweenData"):FindFirstChild("CandyCorn") and plr:FindFirstChild("HalloweenData"):FindFirstChild("OwnsBroomstick") then
local cc = plr:FindFirstChild("HalloweenData"):FindFirstChild("CandyCorn")
local ob = plr:FindFirstChild("HalloweenData"):FindFirstChild("OwnsBroomstick")
print("Saving...")
candySave:SetAsync(plr.UserId, tonumber(cc.Value))
print("Saved")
end
end
end)
Thank you in advance!

2 comments
Are you sure the game isn't shutting down before it gets the chance to save?
Should I assume that were the case (which is most likely true), how would I allow it to save before it closes? Correct me if I am wrong, but :BindToClose should not be used when run in Studio (I am testing in Studio).