My datastore won't save its data. I am making a playtime leaderboard but it won't save data when rejoined. The output says the error message in warn(errormessage)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local playtime = Instance.new("IntValue")
playtime.Name = "Playtime"
playtime.Parent = leaderstats
local data
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-playtime")
end)
if success then
playtime.Value = data
else
print("Player Playtime error whrist saving")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-playtime",player.leaderstats.Playtime.Value)
end)
if success then
print("Player Playtime saved")
else
print("Player Playtime error whrist saving")
warn(errormessage)
end
end)
ammount = 1
timedelay = 1
currencyname = "Playtime"
while true do
wait(timedelay)
for i,v in pairs(game.Players:GetPlayers()) do
if v:FindFirstChild("leaderstats") then
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + ammount
end
end
end

2 comments
Use the game:BindToClose method because if you are trying to save in studio the server will close before it can save the data.
no it wont even save too even on a client