Hi, so I followed a tutorial on youtube on how to save leaderstats data to get more familiar with the topic but when a completely new player joins the game the value of silver is set to 0 even though I specified that it should be set to 50. I'm really new to datastores so I have no clue on why this is occurring. If somebody could help me out that would be great. Thanks!

Code:

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"


	local Silver = Instance.new("IntValue")
	Silver.Parent = leaderstats
	Silver.Name = "Silver"
	Silver.Value = 50
	
	local Silverdata
	local Silversuccess, Silvererrormessage = pcall(function()
		Silverdata = SilverSave:GetAsync(player.UserId.."-Silver")
	end)
	
	if Silversuccess then
		Silver.Value = Silverdata
	else
		print("There was an error whilst getting your Silver data")
		warn(Silvererrormessage)
	end
end)



game.Players.PlayerRemoving:Connect(function(player)
	local Silversuccess, Silvererrormessage = pcall(function()
		SilverSave:SetAsync(player.UserId.."-Silver",player.leaderstats.Silver.Value)
	end)
	
	if Silversuccess then
		print("Player Silver Data successfully saved")
	else
		print("There was an error when saving Silver data")
		warn(Silvererrormessage)
	end
end)

5 comments

0

Wait where is the Leaderstats?

0

Wait where is the Leaderstats?

0

If you've tested before, check to see if the stored value is at 0 silver. In other words, your datastore might already have the players data as a key. So remove it and try testing again.

0

I've used one of my alt accounts that have never joined the game before so they never could've had a data store on the game, but it still gave them 0 silver.

0

Try taking out the first silver value and on line 18 "Silver.Value = (Silverdata) or 50". Or try what YTRaulByte said and rename your datastore wherever you call for it.