Leaderstats not saving for some reason

Hey guys!! Haven’t been here in a while, I’ve been working on a game lately mainly building everything, but today I figured I would start some of the scripting for the game. Just my luck, of course the first thing I do, the leaderstats script is what doesn’t work, and I feel as if all my posts are about leaderstats :joy: it works, just wont save for some reason. I’ve tried multiple different scripts and different ways for it \ because of this and nothing wants to work. Can anyone help?

Heres the saving part of the script!

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Coins.Value = Data.Coins
		Challenges.Value = Data.Challenges
		
	else
		print("There Was An Error Saving"..Player.UserId)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Coins"] = Player.leaderstats.Coins.Value;
		["Challenges"] = Player.leaderstats.Challenges.Value;

	})


end)
6 Likes

Try putting your save lines in a pcall, it may reveal some errors

local success, message = pcall(function()
	Data = DataStore:GetAsync(Player.UserId)
end
if not success then
	warn(message)

if Data then
	Coins.Value = Data["Coins"]
	Challenges.Value = Data["Challenges"]

else
	print("There Was An Error Saving"..Player.UserId)
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)
3 Likes

whats the full script?

You should try adding print statements at certain spots to debug the code.

You could also add this piece of code to the end so it has time to save it:

game:BindToClose(function()
   task.wait(2)
end)
2 Likes

Hey! Thank you, just tried this, both the data in the second line and the if in the 4th line are underlined.

Full Script:

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

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 Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Coins.Value = Data.Coins
		Challenges.Value = Data.Challenges
		
	else
		print("There Was An Error Saving"..Player.UserId)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Coins"] = Player.leaderstats.Coins.Value;
		["Challenges"] = Player.leaderstats.Challenges.Value;

	})


end)

I will also try adding more prints and the BindToClose part!

2 Likes

My mistake, I forgot to add an end) there. Also it’s fine if Data is underlined, it just means it doesn’t have local so it’s getting a little mad but it won’t affect your code.

Here’s the fixed script

local success, message = pcall(function()
	Data = DataStore:GetAsync(Player.UserId)
end)
if not success then
	warn(message)
end

if Data then
	Coins.Value = Data["Coins"]
	Challenges.Value = Data["Challenges"]

else
	print("There Was An Error Saving"..Player.UserId)
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)
3 Likes

Ahh ok, figured about the Data part. Just tested it, still doesn’t seem to be working, and no errors in the script or output either.

1 Like

And just to make sure, are you putting this code in a serverscript or localscript?

1 Like

Server Script in ServerScriptService yes!

I’m wondering if maybe it is having this saving issue because both of the currencies are being given to the player by another script (when a “challenge” is completed)… but that doesnt make much sense

Okay, before line 8 if Data then, can you put print(Data) so we can see what’s being pulled from the datastore?

1 Like

Ah I see, is that other script a localscript or serverscript? That could be causing a difference between the server and client

2 Likes

what does the data print when you view it after player is being added?

2 Likes

That other script is also a server script. I had tried it earlier in a local script for something else and I don’t think it worked, I can try it again for this.

Heres what was printed.

19:44:44.564   ▼  {
                    ["Challenges"] = 0,
                    ["Coins"] = 0
                 }  -  Server - leaderstats:29
2 Likes

If you don’t mind, can you show me both full scripts so I can try to find any errors

2 Likes

Sure! Heres the leaderstats as it is now

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

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 not success then
		warn(message)
	end
print(Data)
	if Data then
		Coins.Value = Data["Coins"]
		Challenges.Value = Data["Challenges"]

	else
		print("There Was An Error Saving"..Player.UserId)
	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)
end)

Then heres the coin giver script, the second half was its own script but i merged them, just teleports the player

local Teleporter = game.Workspace.Island1.MainTeleporting
local Debounce = false
local TimeToWait = 10
amnt1 = 100
amnt2 = 1
function onTouched(part)

	if Debounce == false then
		local h = part.Parent:findFirstChild("Humanoid")
		if (h~=nil) then
			local thisplr = game.Players:findFirstChild(h.Parent.Name)
			if (thisplr~=nil) then
				local leaderstatsstats = thisplr:findFirstChild("leaderstats")
				if (leaderstatsstats~=nil) then
					local coins = leaderstatsstats:findFirstChild("Coins")
					local Challenges = leaderstatsstats:FindFirstChild("Challenges")
					if (coins~=nil) then
						coins.Value = coins.Value + amnt1
						if (Challenges~=nil) then
							Challenges.Value = Challenges.Value + amnt2
							Debounce = true
							wait(TimeToWait)
							Debounce = false
						else
							print("no money 4 u")




						end
					end
				end
			end

		end

	end
end

script.Parent.Touched:Connect(onTouched)


script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		local Teleporting = Player.Character:FindFirstChild("CurrentlyTeleporting")
		
		if not Teleporting then return end
		
		if not Teleporting.Value then
			Teleporting.Value = true
			Player.Character.HumanoidRootPart.CFrame = Teleporter.CFrame + Vector3.new(0,5,0)
			wait(3)
			Teleporting.Value = false
			

		end
	end
end)
1 Like

I think I found your issue :grin:
The function is actually :FindFirstChild(), with a capital F in the beginning, and because you misspelled it, the if statement after it will not reach its condition.

Make sure to fix that issue for all the lines there, I can see several of them.

1 Like

Omg! I will look at that now. Dont know how i missed that lol

And… it didnt work :confused:

1 Like

Did you fix it for this one too?

1 Like

I might be wrong, but it seems like you are trying to save a dictionary to a datastore. Try using HttpService:JSONEncode() and HttpService:JSONDecode() when you are saving and loading the data.

Here are some examples (sorry for the bad formatting)

		local success, errormessage = pcall(function()
			local Data = {
				["Coins"] = Player.leaderstats.Coins.Value;
				["Challenges"] = Player.leaderstats.Challenges.Value;
			}
			local EncodedData = game:GetService("HttpService"):JSONEncode(Data)
			DataStore:SetAsync(Player.UserId, EncodedData)
		end)
	local success, message = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)
	if success then
		DecodedData = game:GetService("HttpService"):JSONDecode(Data)
	end
2 Likes

As far as I know, there is no issue with saving dictionaries to datatsore. I have made several datastore scripts using dictionaries and they worked completely fine.

But OP you can still try this if nothing works

3 Likes