Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why my data saving scripts sometimes work but most of times not?

local dataStoreService = game:GetService("DataStoreService")

local timeData = dataStoreService:GetDataStore("TimeData")

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

local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Time = Instance.new("IntValue",leaderstats)
Time.Name = "Time"
local success , getValue = pcall(function()
	return timeData:GetAsync(player.UserId) or 0
end)
if success then
	print("the load was successful")
else
	warn(getValue)
end
Time.Value = getValue
print ("Loaded Value "..getValue)

game.Players.PlayerRemoving:Connect(function(player)
	local succes , erra = pcall(function()
		timeData:UpdateAsync(player.UserId,function(wtf)
			if Time.Value > wtf then
				return Time.Value
			end
		end)
	end)
	if not succes then
		warn(erra)
	end
	print(player.Name.." has left the server".."("..player.UserId..")")
end)

end)

1 answer

✓ Accepted answer

PlayerRemoving event as datastore saving is unreliable when the last player leaves (without game:BindToClose()). especially when u test it out in studio

playerRemoving doesnt work in the following conditions:

  • The last player leaves
  • The developer manually shut them down via the game page
  • A network error is encountered and the server is forced to shut down

see this article how to properly save

2 comments

0

I love u

0

thanks, if this helped you can mark this as answer :D

This discussion is preserved as it was. New answers, comments, and votes are closed.