I Have an issue with my datastore leaderstats

I have a datastore leaderstats and its not working
I tried all things but nothing changed can someone help here is the code:


local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("playerMoney")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderboard"
	leaderstats.Parent = player
		
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	local data 
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."-money") 
	end)
	
	if success then 
		money.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.."-money", player.leaderstats.Money.Value)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end
	
end)

1 Like

It is simple, go to game settings, go somewhere, if you see “Allow API Services”, turn that on. It will make the datastore have access to your leaderstats and saving it, hopefully it fixes it.

Found the issue.
It is because the server was closing before the data could be saved, add this to the end of the script and move the data saving to a function.

game:BindToClose(function()
--call data saving function here for each player
wait(10) --the max is 30 seconds
end)

Also at line 30, you do player.leaderstats but the name of the folder containing the data is called leaderboard.

Well, i didn’t do it with that bindToClose function and it still saved my data in a game i developed at, his saying his data won’t save, if you stop in run mode it still counts as exitting the game thus PlayerRemoved, it is probably because the API mode he didn’t turn on which is compatible with datastore.

The main issue is that he has tried to save player.leaderstats.Money.Value but the data container is named leaderboard instead which is why it is not saving.
It is also better practice to use BindToClose just incase.

1 Like

Oh you’re right but why didn’t you mention that first? Anyways, his issue is solved, he could’ve atleast send a screenshot of the error output.

I had mentioned that below the code.
Also it wasn’t outputting anything as it had shut down before anything could be printed.

My bad.

(writing 30 more letters so i can send this WOOOOSH)

1 Like

I tried it but its not working

I turned on the API not working again…

This is what I did instead.

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("playerMoney")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderboard"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats

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

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

local function SavePlayerData(player)
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.."-money", player.leaderboard.Money.Value)
	end)

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

game.Players.PlayerRemoving:Connect(SavePlayerData)


game:BindToClose(function()
	for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
		SavePlayerData(Player)
	end
	wait(5)
end)

I also named the leaderstats to the correct naming so it will appear on the playerlist

I want leaderstats will be invisible on playerlist and visible on gui also tried and not working in studio

Any errors? This worked for me in testing. Also has @reygenne1 said, did you remember to turn on DataStore API Access?

I turned on but its not working

Any errors in output? (aaaaaa)

No there is not an error in output

The code I sent does work for me, are you able to send your place over?

What do you mean to say

send your place over?

Save your game and upload it here or you can just send the new code. My best guess is that another script is conflicting it.

Here is the new code

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("playerMoney")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderboard"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats

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

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

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

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

game.Players.PlayerRemoving:Connect(SavePlayerData)


game:BindToClose(function()
	for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
		pcall(function()
			SavePlayerData(Player)
		end)
	end
	wait(5)
end)