Greetings. I have made a simple script for my game to save up points, but for a reason it won’t save them. I have tried some others scripts, but they also didn’t work. The script that I firstly created was:
game.Players.PlayerRemoving:Connect(function(player)
local ID = pointsName .. "-" .. player.UserId -- pointsName = "Points"
DataStore:SetAsync(ID, player.leaderstats.Points.Value) -- I also tried with player.leaderstats[pointsName].Value
print(player.Name .. "'s points were saved.")
end)
You should not save data directly from a leaderstat, because players can modify them. Keep your actual points value saved in a table in your script, so only the server has the real value.
Http calls should be wrapped in a pcall, because they can fail.
local success, failMessage = pcall(function()
--your datastore call
end)
Finally, you should probably use UpdateAsync, it’s preferred over SetAsync. (see wiki for reason why)
iirc, data stores do not work within Roblox Studio unless you explicitly enable them. Try testing in a real server. Also, are any errors printed to output?
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local ID = "Points-"..player.UserId
local pointsValue = DataStore:GetAsync(ID)
local points = player.leaderstats.Points
points.Value = pointsValue or 0
-- ^ you could do this while creating the instance
end)
remember to use protected calls for DataStore requests