Datastore not saving

Whenever the users collects a gorilla it’s supposed to update the gorilla leaderstat value and the save the value when I first joined and collected one it updated my leaderstat value however when I rejoined the game my leaderstat value was reset back to 0, does anyone know what the issue is? (New in datastoreservice, and I have api services enabled)

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

	local gorilla = Instance.new("IntValue")
	gorilla.Name = "Gorillas"
	gorilla.Value = 0
	gorilla.Parent = stat

	local data
	local success, errormessage = pcall(function()
		data = game:GetService("DataStoreService"):GetDataStore("GorillaData"):GetAsync("gorillas-"..player.UserId)
	end)

	if success then
		gorilla.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		game:GetService("DataStoreService"):GetDataStore("GorillaData"):SetAsync("gorillas-"..player.UserId, player.leaderstats.Gorillas.Value)
	end)

	if not success then
		warn("Failed to save gorilla count for player "..player.Name..": "..errormessage)
	end
end)
game.ReplicatedStorage.Badge.OnServerEvent:Connect(function(plr, badge)
	local Badgeservice = game:GetService("BadgeService")
	local gorillas = plr:WaitForChild("leaderstats"):WaitForChild("Gorillas")
	if not Badgeservice:UserHasBadgeAsync(plr.UserId, badge) and badge ~= 0 then
		Badgeservice:AwardBadge(plr.UserId, badge)
		gorillas.Value += 1
	end
end)
5 Likes

Maybe this?

4 Likes

I tried following the previous solution he used but it seem it didnt work or im just dumb

4 Likes

Try setting the Gorrila datastore to a variable above that way the server does not need to retrieve it each time.

Also have you tried testing this in an actual server instance, or just in studio?

4 Likes

So like this? And yes I have tested it both in studio and in actual server

local Storage = game:GetService("DataStoreService"):GetDataStore("GorillaData")

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

	local gorilla = Instance.new("IntValue")
	gorilla.Name = "Gorillas"
	gorilla.Value = 0
	gorilla.Parent = stat

	local data
	local success, errormessage = pcall(function()
		data = Storage:GetAsync("GorillaData" .. player.UserId)
	end)

	if success then
		gorilla.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		Storage:GetAsync("GorillaData" .. player.UserId)
	end)
	
	if not success then
		warn("Failed to save gorilla count for player " .. player.Name .. ": "..errormessage)
	end
end)
2 Likes

Something like that. Also print the player’s final gorilla number when you save.

2 Likes

In the PlayerAdded function instead of doing just if success then try doing if success and data then instead and try it again

1 Like

instead of

if success then
   ...
end

do

if success and data then
   ...
end
1 Like

doesn’t seem to do much when I attempted it

2 Likes

I dont think there’s a need to print the final gorilla number when you “save” because even if i printed it the leaderstat will get reseted back to zero when i rejoin the actual server

2 Likes

It is just to see if the final gorilla value is actually the number we want.

Print the data after the PlayerAdded and the fetching of the data. Smth gives a good point.

1 Like

do you think this is the issue instead? i dont really understand what it means
image

2 Likes

if it is still not saving you can try doing this

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

basically give the datastore a bit of time to actually save

4 Likes

no a lot of people have that and they are working on it. Idk if it affects datastores doe not sure

2 Likes

where do I put this, I just placed it at the end of the script

2 Likes

yes the bottom of the script would be fine

2 Likes

Well doesn’t seem to work when I tried

1 Like

hmm that is weird what type of script is it and where is it located

1 Like

Located in serverscriptservices and is a regular server sided script

Do you get any warnings or errors, stuff like that. Are you sure that you turned on API services in the game settings

1 Like