GUI text not showing leaderstat value?

I am trying to make it so a gui text shows you a leaderstat value because it won’t show normally on mobile, so I created a script that does that but got error 13:02:06.242 - leaderstats is not a valid member of Player
Script for gui text:

local cash = game.Players.player.leaderstats:WaitForChild("Gold")

function changed()
	script.Parent.Text = "Gold: $"..cash.Value
end

cash.Changed:connect(changed)

Script for leaderstat (datastore):

local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore")


game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Money = Instance.new("IntValue")
	Money.Name = "Gold"
	Money.Parent = leaderstats
	
	
	local key = "user-" .. player.userId
	
	local storeditems = datastore:GetAsync(key)
	if storeditems then
		Money.Value = storeditems[1]
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Gold.Value}
	local key = "user-" .. player.userId
	
	datastore:SetAsync(key, items)
end)
1 Like

leader stats is probably not loaded in yet. Try

local cash = game.Players.player:WaitForChild("leaderstats"):WaitForChild("Gold")
1 Like

getting error: 13:11:53.362 - player is not a valid member of Players

If its in a LocalScript, you need to use LocalPlayer

local cash = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Gold")
1 Like

I am doing a script not localscript, should I be using alocal script for this?

Your Datastore script should be a Script in ServerScriptService.
Your Gui Script should be a LocalScript in The Player Gui or near

1 Like

Using scripts for gui manipulation is a bad practice - If you make a change to a gui on the client, that server script won’t be able to see the change. You should be using a local script for this

1 Like

I did not know that, thank you!