Leaderstats Cash GUI

I was scripting so much. Then I had to put all my scripts in a different game and finally found what the issue is.

CashLabel = script.Parent.Parent.Parent.Cash.CashValue
Leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats").Cash.Value

while true do
	wait()
	CashLabel.Text = "$ "..Leaderstats
end

This local script is the issue, any solutions why it won’t show the value on the cash gui text label?

You didn’t reference the value correctly and you should use events instead.

Try this:

--//Services
local Players = game:GetService("Players")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Cash = LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")
local CashLabel = script.Parent.Parent.Parent.Cash.CashValue

--//Functions
local function SetText()
	CashLabel.Text = "$ ".. Cash.Value
end

SetText()

Cash.Changed:Connect(function()
	SetText()
end)

Hmmm, let me try something similar to that. Hmm

So like this??

local CashLabel = script.Parent.Parent.Parent.Cash.CashValue
local Leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")

local function TextValue()
	CashLabel.Text = "$ "..Leaderstats.Cash.Value
end

Leaderstats.Changed:Connect(function()
	TextValue()
end)

No, you have to connect the event to a value, not a folder. You can just copy my code and it should work.

What do you mean by that?

I want to figure out how to do it, since it would be helpful for me to know in the future.

local CashLabel = script.Parent.Parent.Parent.Cash.CashValue
local Leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")

local function TextValue()
	CashLabel.Text = "$ "..Leaderstats.Cash.Value
end

Leaderstats.Changed:Connect(function()
	TextValue()
end)

Like this?

Yes, but you don’t have to add another .Cash and you should run the function at the start. You should also use :GetService.

New code:

local Players = game:GetService("Players")

local CashLabel = script.Parent.Parent.Parent.Cash.CashValue
local Leaderstats = Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")

local function SetText()
	CashLabel.Text = "$ ".. Leaderstats.Value
end

SetText()

Leaderstats.Changed:Connect(function()
	SetText()
end)
1 Like

But I don’t need the player service…

Ahh, you need the player service for leaderstats variable

You do, you’re accessing LocalPlayer.

So like this?

local Player = game:GetService("Players")
local CashLabel = script.Parent.Parent.Parent.Cash.CashValue
local Leaderstats = Player.LocalPlayer.WaitForChild("leaderstats"):WaitForChild("Cash")

local function TextValue()
	CashLabel.Text = "$ "..Leaderstats.Value
end

TextValue()

Leaderstats.Changed:Connect(function()
	TextValue()
end)

wait oops see a error brb ADDING CHARCTERS

I didn’t do :waitforchild correctly

Ok it’s working. Question why can’t I just do game.Players.LocalPlayer and I have to do the player service??

local Player = game:GetService("Players")
local CashLabel = script.Parent.Parent.Parent.Cash.CashValue
local Leaderstats = Player.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")

local function TextValue()
	CashLabel.Text = "$ "..Leaderstats.Value
end

TextValue()

Leaderstats.Changed:Connect(function()
	TextValue()
end)

It’s just because when you use game.Players, and an exploiter or someone names the Players service as for example apples, your script will error. If you use game:GetService though, this won’t happen because it doesn’t index directly through game, which is why it’s more efficient and safe.

If my post worked, please set it as the solution.

Do I make ALL my scripts have the Player service?

No, it’s just when you’re getting a service, make sure to use :GetService, instead of game.ServiceName.

Hmm, when should I use game:getserivce(“Players”)
Sincea lot of my scripts have the player but just doing game.Players

Define the Players service at the top (like what I did) and then just retrieve it later.