Why does this sometimes not save?

this works but sometimes the data doesn’t save in busier servers

local DataStoreService = game:GetService(“DataStoreService”)

local playerData = DataStoreService:GetDataStore(“playerData”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player.leaderstats

local gold = leaderstats.Points

local data 
local success, errormessage = pcall(function()
	data = playerData:GetAsync(player.UserId.."-gold") 
end)

if success then 
	gold.Value = data
else
	print("Error while getting your data")
	warn(errormessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
	playerData:SetAsync(player.UserId.."-gold", player.leaderstats.Points.Value)
end)

if success then
	print("Data successfully saved!")
else
	print("There was an error while saving the data")
	warn(errormessage)
end

end)

any suggestions?

1 Like