Help with cash label

Hello.
How would I make a text label appear on a players screen when they earn/lose cash?

if you have a value for their cash amount (in leaderstats for example), you can reference to that in a localScript, you can then use GetPropertyChangedSignal or Changed, like so:

--LocalScript inside of the textLabel you want to have appear
local lastVal
local cash = game:GetService("Players"):WaitForChild("leaderstats"):WaitForChild("Cash")

local function cashChanged()
    if lastVal then
        if cash.Value < lastVal then
            script.Parent.Visible = true
            delay(3, function() script.Parent.Visible = false end))
        end
    end
    lastVal = cash.Value
end

cash:GetPropertyChangedSignal("Value"):Connect(cashChanged)
1 Like

Thanks, also, one question. I forgot to ask it above. How would I make it red with a “-” in front of it if the amount was deducted from the player’s balance and how would I make it green with a "+’ if the amount was added to the player’s balance?

Change what happens in after the If lastVal then, something like this:

--After the if statement
local diff = cash.Value - lastVal
if diff > 0 then
    script.Parent.Text = "+"..tostring(diff)
    script.Parent.TextColor = Color3.new(0,1,0)
else
    script.Parent.Text = "-"..tostring(diff)
    script.Parent.TextColor = Color3.new(1,0,0)
end
delay(3, function() script.Parent.Visible = false end))
1 Like

Thanks for the help!

(trying to get to the word limit)

Also, what’s the “local lastVal”? You never gave it a value.

Just set it to 0 for the starting value, and set it near the top of the script

lastVal receives a value later in the script. I declared it at the top for scope reasons, but it’s only ever set to a value in the function, ~line 11.

It needs to be set to nil (which is what happens when you leave it blank), so that the if lastVal then statement works the first time. If it has a value, then the first time the function runs it will think the player gained a lot of money. You could alternatively set it to the default starting cash value or find another way to set it to the player’s cash value when they join the game.

It doesn’t seem to work.
image
There are no errors either.

did you have the cash value change twice? also use “task.delay” rather than “delay” (the deprecated “delay” has throttling issues if used in mass) and make sure the frame / screengui is enabled and visible

Just use “.Changed” for ValueBase instances (the base class for instances suffixed with the word “Value”, i.e; IntValue, StringValue, NumberValue etc.) it’s more intuitive, like GetPropertyChangedSignal("Value") it only fires when the “Value” property changes and you get the added benefit of the “new value” parameter which represents the new value of the instance’s “Value” property which resulted in the event firing. Take the following for example.

local players = game:GetService("Players")
local player = players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local cash = leaderstats:WaitForChild("Cash")

cash.Changed:Connect(function(newCash)
	print(newCash)
end)

newCash would print “Value” since that parameter is the “property that was changed”.

You’d want to do print(cash.Value) instead of print(newCash).

Nevermind, roblox changed it.

They didn’t change anything, this is how “.Changed” has always worked for instances which are suffixed with the word “Value”. You’re think of the “.Changed” event for regular instances.

Figured it out.

local lastVal
local cash = game.Players.LocalPlayer.leaderstats.Cash
local dif = 0

cash.Changed:Connect(function()
	if dif ~= cash.Value then
		script.Parent.Visible = true
		script.Parent.Text = "+".. cash.Value - dif
		dif = cash.Value
	end
end)
local cash = game.Players.LocalPlayer.leaderstats.Cash
local label = script.Parent
local oldCash = 0

cash.Changed:Connect(function(newCash)
	label.Visible = true
	label.Text = "+"..(newCash - oldCash)
	oldCash = newCash
end)

question, how would I make it change to red/green depending if the cash was subtracted or added?
this is my current code for it:

local cash = game.Players.LocalPlayer.leaderstats.Cash
local label = script.Parent
local oldCash = 0

cash.Changed:Connect(function(newCash)
	if oldCash ~= newCash then
		label.Visible = true
		if oldCash > 0 then
			script.Parent.TextColor = BrickColor.new(0.0941176, 0.941176, 0)
			script.Parent.Text = "+".. newCash - oldCash
		elseif oldCash < 0 then
			script.Parent.TextColor = BrickColor.new(221, 0, 0)
			script.Parent.Text = newCash - oldCash
		end
		oldCash = newCash
		wait(0.5)
		label.Visible = false
	end
end)

It only switches to red if the current balance is in the negatives.

Nevermind, I figured it out.
I added a different variable which is new cash and old cash.

local cash = game.Players.LocalPlayer.leaderstats.Cash
local label = script.Parent
local oldCash = 0

cash.Changed:Connect(function(newCash)
	if oldCash ~= newCash then
		local diff = newCash - oldCash
		label.Visible = true
		if diff > 0 then
			script.Parent.TextColor = BrickColor.new(0.0941176, 0.941176, 0)
			script.Parent.Text = "+".. newCash - oldCash
		elseif diff < 0 then
			script.Parent.TextColor = BrickColor.new(221, 0, 0)
			script.Parent.Text = newCash - oldCash
		end
		print(oldCash, newCash)
		oldCash = newCash
	end
end)