Datastores are not working

I wanted to use the same leaderboard saving script that was in my other game working and its not working. (Windows 10 OS [Operating System] - Roblox)
Since its gui-game, with not visible leaderboard and players won’t see it, i needed this for something other

Script:

Normal Script, Workspace

local DataStoreService = game:GetService("DataStoreService")
local pdata = DataStoreService:GetDataStore("playerDatamogus23")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local value = Instance.new("IntValue")
	value.Name = "Times"
	value.Parent = leaderstats
	
	local data 
	local success, errormessage = pcall(function()
		data = pdata:GetAsync(player.UserId) 
	end)
	
	if success then 
		value.Value = data
	else
		print("there was an error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		pdata:SetAsync(player.UserId, player.leaderstats.Times.Value)
	end)
	
	if success then
		print("your data got saved wheres my thanks")
	else
		print("there was an error")
		warn(errormessage)
	end
	
end)

I already enabled Studio access to API services

2 Likes

Add this to your code and see if it fixes the problem:

game:BindToClose(function()
    task.wait(3)
end)

Ik that i’m answering 2 hours after but i should assign whole code to this function?

When using BindToClose,
you’ll need to save their data there too.
[Loop through all players is a possible option]

As you did in the PlayerRemoving :

for _,Player in pairs(game.Players:GetPlayers()) do

   local success, errormessage = pcall(function()
	   pdata:SetAsync(Player.UserId, Player.leaderstats.Times.Value)
   end)
	
   if success then
	   print("your data got saved wheres my thanks")
   else
	   print("there was an error")
	   warn(errormessage)
   end
end
1 Like

You should also try putting the script in serverscriptservice

i did, nothing changed, still not working

Here too, nothing changed
idk what could be wrong

Is it a local or server script?

Make sure this is enabled

Its a server script,
and i already enabled that thing

1 Like

Have you tried changing the value in the server side and not client side

Yes i do

characters lol

I just tested it out and it works nicely.

Notes:
1.In studio, data not always saves [ test it in-game].
2.Are you changing your time somewhere else in the game? If so, please show us where.

What do you mean about changing my time somewhere else in game?
and i am testing it in game already

Did you read this topic on data stores? Datastore Tutorial for Beginners or you got it from somewhere else?

No i didnt, i ‘learned’ it by something else

1 Like

I probably found an error, with this script is everything okay, its something wrong with my testing system (just textbutton that adds value). When i am doing that in this script by adding while true do loop that sets value to old value + 1 everything is working. Thanks for your help I will try to make it work as I want.

2 Likes