Leaderstats not saving for some reason

Yeah, I fixed all four of them

1 Like

This might be unrelated to the issue but the loading part of your script has a small issue. If a player has no data, it will print an error message. Do something like this instead:

	local success, message = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)
	if success then
		if Data then
			Coins.Value = Data["Coins"]
			Challenges.Value = Data["Challenges"]
			--perform the rest of the script
		else
			print("Player has no data yet")
		end
	else
		warn("Error with loading data. Error: "..message)
	end

This is the part from your original script that I was talking about:

original script
if Data then
	Coins.Value = Data["Coins"]
	Challenges.Value = Data["Challenges"]
else
	print("There Was An Error Saving"..Player.UserId)
end
1 Like

It is saving correctly if this is what prints, you are most likely not changing the values of the leaderstats correctly. Is the script changing the values client sided?

1 Like

This didn’t work either, unless I added it in completely wrong.

1 Like

What about my previous reply with JSONEncode? Also, you should still keep the script from my previous reply.

1 Like

Everything is being changed in a normal Server script, if that is what you are asking.

1 Like

Im trying it now, a bit confusing for me to put it in :joy:

2 Likes

As I was editing the script for you, I found the reason why your data wasn’t saving (or at least i think i did). You put the .PlayerRemoving event inside of .PlayerAdded

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("CurrencyStats")
local HttpService = game:GetService("HttpService")

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

	local hiddenstats = Instance.new("Folder", Player)
	hiddenstats.Name = "hiddenstats"
	hiddenstats.Parent = Player

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 0
	Coins.Parent = leaderstats
	
	local Challenges = Instance.new("IntValue")
	Challenges.Name = "Challenges"
	Challenges.Value = 0
	Challenges.Parent = leaderstats
	
	local success, message = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)
	if success then
		print(Data)
		if Data then
			Coins.Value = Data["Coins"]
			Challenges.Value = Data["Challenges"]
		else
			print("Player has no data")
		end
	else
		warn(message)
	end
end)


game.Players.PlayerRemoving:Connect(function(Player)
	local success, message = pcall(function()
		DataStore:SetAsync(Player.UserId, {
			["Coins"] = Player.leaderstats.Coins.Value;
			["Challenges"] = Player.leaderstats.Challenges.Value;
		})
	end)
	if not success then
		warn(message)
	end
end)

If that doesn’t work, I’ll edit the script again with JSONEncode and decode.

You can remove the HttpService variable I put on top since you probably don’t need it

3 Likes

Ahh! It worked!! Where in the script was the error btw? And thank you all for the help.

2 Likes

I didn’t test the script in studio, I just noticed it as I was trying to rewrite parts of the script for you.

1 Like

lol i wonder how i didnt notice that either

2 Likes

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