Hovering over text updates incorrect form whilst hovering over text

I have this local script where it locally shows the players cash value. The issue I’m getting for this is when I hover over the text, it shows the abbreviated version. However, when the value for the Cash updates, it reverts back to full form while the mouse is hovering over the text. I have tried using if statement but then it got confusing to the point that it feels wrong. So how do I make it when it updates while hovering over the text, it does not update back to the unabbreviated number, instead of updating it in the abbreviated version? Not a pro scripter but thanks for reaching out.

In conclusion, how to keep it in abbreviated form whilst the mouse hovering over the text?

Full Script:

local suffixes = {"K", "M", "B", "T", "q", "Q"}

function abbreviate(value)
	local negative = (tonumber(value) or 0) < 0
	if negative then
		value = -(value)
	end
	if not (tonumber(value)) or tonumber(value) <= 999 then
		return value
	end
	local str = value
	for i = 1, #suffixes do
		if tonumber(value) < 10 ^ (i * 3) then
			str = math.floor(value / ((10 ^ ((i - 1) * 3)) / 100)) / 100 .. suffixes[i - 1]
			break
		end
	end
	if str and negative then str = "-"..str end
	return str
end

local player = game.Players.LocalPlayer

script.Parent.Text = player.stats.Cash.Value

player.stats.Cash:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent.Text = player.stats.Cash.Value
end)

script.Parent.MouseEnter:Connect(function()

	local player = game.Players.LocalPlayer
	script.Parent.Text = abbreviate(player.stats.Cash.Value)
end)

script.Parent.MouseLeave:Connect(function()

	local player = game.Players.LocalPlayer
	script.Parent.Text = player.stats.Cash.Value
end)

Tried using if statement:

script.Parent.MouseEnter:Connect(function()
	
	if script.Parent.MouseEnter == true and player.stats.Cash:GetPropertyChangedSignal("Value") then
		player.stats.Cash:GetPropertyChangedSignal("Value"):Connect(function()
			script.Parent.Text = abbreviate(player.stats.Cash.Value)
		end)
		else
	local player = game.Players.LocalPlayer
		script.Parent.Text = abbreviate(player.stats.Cash.Value)
		end
end)

Hey!

Try adding a bool value. When the mouse enters set it to true, when the mouse leaves set it to false. Then in the value changed function use the bool value and check if its true or false.
If its true, use the abbreviated number, if its false then dont.

Cheers

Thanks! It actually worked. Did something like this. Hope it isn’t that bad. However, another problem I’m getting is the abbreviated form doesn’t updated, so I’m trying to figure that out rn.

player.stats.Cash:GetPropertyChangedSignal("Value"):Connect(function()
	
	if script.Value.Value == true then return end
	script.Parent.Text = player.stats.Cash.Value
end)

Hey!

You shouldn’t just force end the function when the value is true, use your abbreviate function instead of returning nil.

abbreviate(player.stats.Cash.Value)

Cheers

But then when I hover over the text, it doesn’t show the abbrevaiton unless the value changes, because of GetPropertyChangedSignal. But I could use a loop while true, but that could impact on performance?

Why would you need to check the property signal anyway?

For game optimization? Because I hear while true loops are bad.

No no, why would you need to check the property signal of the cash? You could just check if the mouse had entered, then abbreviate it without checking property signal event.

My bad. So, I tried doing what you did and it seems to work. It updates the text in abbreviation which was what I wanted.

script.Parent.MouseEnter:Connect(function()
	
	script.Value.Value = true
	script.Parent.Text = abbreviateNums(player.stats.Bank.Value)
	
	player.stats.Bank:GetPropertyChangedSignal("Value"):Connect(function()
		
		script.Parent.Text = abbreviateNums(player.stats.Bank.Value)
	end)
end)
1 Like