Cash Display Bug

hello im trying to make a coin flip script but displaying the cash isnt working for some reason.

script:

while wait() do 
	player = game.Players.LocalPlayer
	script.Parent.Text = ("$" ..player.leaderstats.Cash.Value)
end



error:
Players.SakDevRblx.PlayerGui.Coin Flip.coin flip.top.Cash amount.Script:3: attempt to index nil with ‘leaderstats’

this may help:

Alright. Let’s start with a bit of a tip: Try to avoid using while loops for things like this. Instead, try a :GetPropertyChangedSignal() event:

player:WaitForChild("leaderstats"):WaitForChild("Cash"):GetPropertyChangedSignal("Value"):Connect(function()
 -- code here
end)

And now for your solution: Integers and Strings are different kinds of ways to store information. You can, and in this case should, use tostring(player.leaderstats.Cash.Value) to turn that number into text.

Probably because it runs before the cash loads (I’m assuming from the single script), try using waitforchild. Also, try turning the number into a string.

while wait() do 
	player = game.Players.LocalPlayer
    local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	script.Parent.Text = ("$" ..tostring(cash.Value)
end

Edit: I also agree with @d_ytme on not using while loops and instead using :GetProperyChangedSignal() or using the .Changed event. So:

player:WaitForChild("leaderstats"):WaitForChild("Cash").Changed:Connect(function()
	player = game.Players.LocalPlayer
    local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	script.Parent.Text = ("$" ..tostring(cash.Value)
end)

now its saying
Players.SakDevRblx.PlayerGui.Coin Flip.coin flip.top.Cash amount.Script:1: attempt to index nil with ‘WaitForChild’
edit also its ("$" …tostring(cash.Value))
not
("$" …tostring(cash.Value)

Your player object is being identified with nil. i just realized you should make the player variable above the function instead of inside.

i made it above and im still getting same error

Maybe use “local player = game.Players.LocalPlayer” and also is this code inside a local script?

yes. it should also be noted that there are 2 folders named “leaderstats” and both with the “Cash” value.

(It didnt work)

Can I see the script object on the explorer and also is that the whole code you are using? (also try only using 1 leaderstats folder)

inside the function brackets put value remove the cash variable and say

script.Parent.Text = "$"..value 

also reference the player like this:

local Players = game:GetService("Players")

local player = Players.LocalPlayer

and where did you parent the script?(by that I mean in which service)

i replaced everything and the script doesnt work.

the script is parented to the a text label and the text label is inside of a folder in startergui

edit: just in case i wrote script wrong this is most current:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player:WaitForChild("leaderstats"):WaitForChild("Cash").Changed:Connect(function()
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
 script.Parent.Text = "$"..player.Cash.Value 
	end)

yeah its written wrong, put this:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player:WaitForChild("leaderstats"):WaitForChild("Cash").Changed:Connect(function(value)
 script.Parent.Text = "$"..tostring(value)
end)

as you can see cash amount has not updated.

It updates whenever the value gets changes not when the player gets in the game. youll have to put this in the code outside the function:

local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
script.Parent.Text = $..tostring(cash.Value)

please write the entire script

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
script.Parent.Text = "$"..tostring(cash.Value)
cash.Changed:Connect(function(value)
 script.Parent.Text = "$"..tostring(value)
end)

1 Like

you forgot to do “$” on line 4, but it works

you did:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
script.Parent.Text = $..tostring(cash.Value)
cash.Changed:Connect(function(value)
 script.Parent.Text = "$"..tostring(value)
end)

correct form:
local Players = game:GetService(“Players”)
local player = Players.LocalPlayer
local cash = player:WaitForChild(“leaderstats”):WaitForChild(“Cash”)
script.Parent.Text = “$”…tostring(cash.Value)
cash.Changed:Connect(function(value)
script.Parent.Text = “$”…tostring(value)
end)

Yes, I realized after posting and edited it but I’m glad it works.

2 Likes