Datastore not working

Hey, I’m not sure why this isn’t working I’m not getting any errors and everything seems to be right I had another script but it wasn’t working so I tried one from the toolbox as well and none of them worked

Script

local DataStore = game:GetService("DataStoreService")
local CreditData = DataStore:GetDataStore("CreditStats") --Credit Data
local Value = 0

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats= Instance.new("Folder")
	leaderstats.Name= ("leaderstats")
	leaderstats.Parent= player
	-----------------------------------------------------------
	local credit= Instance.new("IntValue")
	credit.Name= ("Neon Credits") --credit Value
	credit.Parent = leaderstats
	credit.Value = 0 -- Starter Value
	credit.Value = CreditData:GetAsync(player.UserId) or Value
	CreditData:SetAsync(player.UserId, credit.Value)
	-----------------------------------------------------------
	------------------------------------------------------------
	game.Players.PlayerRemoving:Connect(function(player)
		CreditData:SetAsync(player.UserId, player.leaderstats["Neon Credits"].Value)
	end)
	
end) 

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		CreditData:SetAsync(player.UserId, player.leaderstats["Neon Credits"].Value)
	end
	
end)




I’ve modified the script a little bit to include a couple of debug statements. Can you join the game then leave, and tell me what the outputs are please?

local DataStore = game:GetService("DataStoreService")
local CreditData = DataStore:GetDataStore("CreditStats") --Credit Data
local Value = 0
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats= Instance.new("Folder")
	leaderstats.Name= "leaderstats"
	leaderstats.Parent= player
	-----------------------------------------------------------
	local credit= Instance.new("IntValue")
	credit.Name= "Neon Credits" --credit Value
	credit.Parent = leaderstats

	local Loaded = CreditData:GetAsync(player.UserId)
	if Loaded then
		print(`Loaded the following: {tostring(Loaded)}`)
	else
		print("No data to load. Using default value")
	end
	credit.Value = Loaded or Value
	------------------------------------------------------------
end) 

game.Players.PlayerRemoving:Connect(function(player)
	print(`Saving player's data: {tostring(player.leaderstats["Neon Credits"].Value}`)
	CreditData:SetAsync(player.UserId, player.leaderstats["Neon Credits"].Value)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		print(`Saving player's data (BindToClose): {tostring(player.leaderstats["Neon Credits"].Value}`)
		CreditData:SetAsync(player.UserId, player.leaderstats["Neon Credits"].Value)
	end
end)
1 Like

only thing printed was Saving(player.leaderstats[“Neon Credits”].Value} - Server - MoneyDatastoreAcc:24

Apparently I missed a bracket in the output statement. Try using this one instead

local DataStore = game:GetService("DataStoreService")
local CreditData = DataStore:GetDataStore("CreditStats") --Credit Data
local Value = 0
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats= Instance.new("Folder")
	leaderstats.Name= "leaderstats"
	leaderstats.Parent= player
	-----------------------------------------------------------
	local credit= Instance.new("IntValue")
	credit.Name= "Neon Credits" --credit Value
	credit.Parent = leaderstats
	local Loaded = CreditData:GetAsync(player.UserId)
	if Loaded then
		print(`Loaded the following: {tostring(Loaded)}`)
	else
		print("No data to load. Using default value")
	end
	credit.Value = Loaded or Value
	------------------------------------------------------------
end) 
game.Players.PlayerRemoving:Connect(function(player)
	print(`Saving player's data: {tostring(player.leaderstats["Neon Credits"].Value)}`)
		CreditData:SetAsync(player.UserId, player.leaderstats["Neon Credits"].Value)
end)
game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		print(`Saving player's data (BindToClose): {tostring(player.leaderstats["Neon Credits"].Value)}`)
			CreditData:SetAsync(player.UserId, player.leaderstats["Neon Credits"].Value)
	end
end)

Hey sorry for the late response but the updated code printed out
that it loaded 0 and saved 0 but the I updated the neon credits to be 100, so i think its an error in the saving

When you change the value, are you using a local script or a server script?

im just using the properties panel for the intvalue not any scripts rn

I believe that you are changing it on the client side then, hence why it doesn’t save.
If you click this:
image
while in the game (and in Studio), it should switch over to server side. When in the server side mode, try changing the property and see if it saves now

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.