Tokens not saving in datastore

So i have here a datastore script for my currency ‘Tix’ and it wont save . Can you please help?

local datastoreservice = game:GetService("DataStoreService")

local datastore = datastoreservice:GetDataStore("DATA33333")

game.Players.PlayerAdded:Connect(function(player)
	
	
	
	local tokens = Instance.new("IntValue")
	tokens.Name = "Tix"
	tokens.Value = 0
	tokens.Parent = player
	local data
	
	local success,errorMsg = pcall(function()
		data = datastore:GetAsync(player.UserId)
	end)
	
	if data ~= nil then
	
		if data.Tokens then 
			tokens.Value = data.Tokens
		end
		
	
		
	end
	

		
end)

game:BindToClose(function()
	-- will run when the server is about to shutdown
	for i, player in pairs(game.Players:GetPlayers()) do
		local data = {}
		
		
		
		
		data.Tokens = player.Tix.Value
	
		
		local success,errorMsg = pcall(function()
			datastore:SetAsync(player.UserId,data)
		end)
		
		if success then
			print("Success")
		end
		
		if errorMsg then
			print("Error found while trying to save data (are roblox datastore servers down?)"..errorMsg)
		end
	end
end)

local trapDebounce = false

game.Players.PlayerRemoving:Connect(function(player)
	
	local data = {}
	
	
	data.Tokens = player.Tix.Value
	
	
	
	local success,errorMsg = pcall(function()
		datastore:SetAsync(player.UserId,data)
	end)
	
	if success then
		print("Success")
	end
	
	if errorMsg then
		print("Error found while trying to save data (are roblox datastore servers down?)"..errorMsg)
	end
end)



2 Likes

The SetAsync only runs after the game closes because you’re using game:BindToClose so use Players.PlayerRemoving instead for when the player leaves.

1 Like

Wait why are you even saving the data twice.

1 Like
local datastoreservice = game:GetService("DataStoreService")

local datastore = datastoreservice:GetDataStore("DATA33333")

game.Players.PlayerAdded:Connect(function(player)
	
	
	
	tokens = Instance.new("IntValue")
	tokens.Name = "Tix"
	tokens.Value = 0
	tokens.Parent = player
	
	local success, data= pcall(function()
		datastore:GetAsync(player.UserId .. "TIX")
	end)
	
	if data ~= nil then
	
		if data then 
			tokens.Value = data
		end
		
	
		
	end
	

		
end)

local trapDebounce = false

game.Players.PlayerRemoving:Connect(function(player)
	
	
	
	local success,errorMsg = pcall(function()
		datastore:SetAsync(player.UserId .. "TIX", tokens.Value)
	end)
	
	if success then
		print("Success")
	end
	
	if errorMsg then
		print("Error found while trying to save data (are roblox datastore servers down?)"..errorMsg)
	end
end)

i changed your code try and see if this works

1 Like

BindToClose runs when the game is about to shutdown, so no, you should be perfectly fine with it

says that it runs the functions on 30 seconds perplexed when closing

1 Like

Hes saving the data twice which is unnecessary.

1 Like

It only runs when the game closes and not when the player leaves so this would cause problems in a multiplayer game.

1 Like

That isn’t twice, y’know, it’s Server not Client shutting down. And the server might struggle with it. I have the same building scheme as this guy does, but it works perfectly fine for me.

1 Like

BindToClose runs when the server shutdowns so if you have 5 players in a server and if 1 leaves then BindToClose won’t run.

1 Like

I just tested it and the issue is nothing it works fine :confused:

1 Like

Was this supposed to be a leaderstats script? If it was you forgot to add a folder, named leaderstats.

You’ll have to put the IntValue inside the folder.

And edit these values.

data.Tokens = player.Tix.Value

to

data.Tokens = player.leaderstats.Tix.Value
1 Like

its not supposed to go into a leaderstats folder

i think a reason why its not saving is maybe different other scripts in my game might of made it not save.