Help: Pop-up Gui Currency Error

I have an error with this script:

local Players = game:GetService("Players")
local plr = script.Parent.Parent.Parent
local leaderstats = plr:WaitForChild("leaderstats")
local cur = "Cash"
local Coins = leaderstats:WaitForChild(cur)
local dif = Coins.Value

local suffixes = {'','K','M','B','T','qd','Qn','sx','Sp','O','N','de','Ud','DD','tdD','qdD','QnD','sxD','SpD','OcD','NvD','Vgn','UVg','DVg','TVg','qtV','QnV','SeV','SPG','OVG','NVG','TGN','UTG','DTG','tsTG','qtTG','QnTG','ssTG','SpTG','OcTG','NoAG','UnAG','DuAG','TeAG','QdAG','QnAG','SxAG','SpAG','OcAG','NvAG','CT'}
local function format(val)
	for i=1, #suffixes do
		if tonumber(val) < 10^(i*3) then
			return math.floor(val/((10^((i-1)*3))/100))/(100)..suffixes[i]
		end
	end
end

Coins.Changed:Connect(function(NewValue)
	if NewValue - dif > 0 then
		local random = math.random(1, 900)
		local xnew = random / 1000
		local new = script:WaitForChild(cur):Clone()
		new:WaitForChild(cur.."Info").Text = (format(NewValue - dif)) --Error Here
		local NewRandom = Random.new()
		new.Position = UDim2.new(NewRandom:NextNumber(0, 0.9), 0, NewRandom:NextNumber(0, 0.9), 0)
		new.Parent = script.Parent
		dif = Coins.Value
		local TweenService = game:GetService("TweenService")

		local tween = TweenService:Create(new, TweenInfo.new(1), {Rotation = 360})

		task.wait(1)
		new:TweenSize(UDim2.new(0.056, 0, 0.099, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.3, true)
		task.wait(0.3)
		new:TweenPosition(UDim2.new(0.025, 0,0.305, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
		task.wait(0.2)
		new:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.3, true)
		task.wait(0.2)

		new.Parent = nil
		dif = NewValue
	elseif dif - NewValue < 0 then
		print("Lost "..(NewValue - dif)..": Amount of Money")
	end
end)

Video:

It gives me 1k in the leaderstats but it shows in screen it gives me 2k and some times 3k
How to fix it?
Sry my pc sucks and My pants got deleted

2 Likes

Maybe there’s something wrong with the format function? I can’t really tell, but try debugging by putting something like

print(NewValue - dif)

before the error line to see if thats where the number is off or

print(format(1000)) -- if it doesn't print 1k then there's something wrong with the format function

somewhere inside the script.

In general, print() is a really good tool for debugging to see where values are off and where a script might stop running.

Hope I could help

U set dif as coins.Value, then u set it as NewValue after 1.5 seconds so maybe you should try removing the line where u set dif to NewValue, and also instead instead of setting new parent to nil just use new:Destroy()

1 Like

image

image

The format code is correct and it parts 1k

But NewValue - dif is have an error
I don’t know how to fix it

Like that: dif = NewValue
???

When I did it:
image

image

Ok I think I know why that happens. See, in the code when you call that function if a coin value is changed you have a tween section which takes around 2 seconds to run and you set the dif to NewValue after that. So lets say for example dif = 15000 and you click and NewValue is 16000, the first Ui will show 1k, but if you press the screen again before the tween finishes and dif becomes = 16000, you will get NewValue = 17000 and dif would not have had the time to switch to 16000 from before because of the 2 second wait so it will still be 15000 and therefore 17000-15000 will print out 2000. I think I fixed it. Try this code and see if it works now.

Coins.Changed:Connect(function(NewValue)
	if NewValue - dif > 0 then
		local random = math.random(1, 900)
		local xnew = random / 1000
		local new = script:WaitForChild(cur):Clone()
		new:WaitForChild(cur.."Info").Text = (format(NewValue - dif)) --Error Here
		local NewRandom = Random.new()
		new.Position = UDim2.new(NewRandom:NextNumber(0, 0.9), 0, NewRandom:NextNumber(0, 0.9), 0)
		new.Parent = script.Parent
		dif = Coins.Value
		local TweenService = game:GetService("TweenService")
        coroutine.wrap(function()

		   local tween = TweenService:Create(new, TweenInfo.new(1), {Rotation = 360})

		   task.wait(1)
		   new:TweenSize(UDim2.new(0.056, 0, 0.099, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.3, true)
		   task.wait(0.3)
		   new:TweenPosition(UDim2.new(0.025, 0,0.305, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
		   task.wait(0.2)
		   new:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.3, true)
		   task.wait(0.2)

		   new.Parent = nil
        end)()
		dif = NewValue
	elseif dif - NewValue < 0 then
		print("Lost "..(NewValue - dif)..": Amount of Money")
	end
end)

Basically I added the tween part inside a coroutine so that it will run while the rest of the script also runs instead of stopping it.

Coroutines can be overall very beneficial and save you a lot of time.

1 Like

That works!
But I edited the script and the correct and simple script is:

local Players = game:GetService("Players")
local plr = script.Parent.Parent.Parent
local leaderstats = plr:WaitForChild("leaderstats")
local cur = "Cash"
local Coins = leaderstats:WaitForChild(cur)
local module = require(game.ReplicatedStorage.Abbreviations)
local dif = Coins.Value


Coins.Changed:Connect(function(NewValue)
	if NewValue - dif > 0 then
		print(NewValue - dif.."old")
		local random = math.random(1, 900)
		local xnew = random / 1000
		local new = script:WaitForChild(cur):Clone()
		new:WaitForChild(cur.."Info").Text = module.abbreviate(NewValue - dif)
		local NewRandom = Random.new()
		new.Position = UDim2.new(NewRandom:NextNumber(0, 0.9), 0, NewRandom:NextNumber(0, 0.9), 0)
		new.Parent = script.Parent
		dif = Coins.Value
		print(NewValue - dif)
		local TweenService = game:GetService("TweenService")

		local tween = TweenService:Create(new, TweenInfo.new(1), {Rotation = 360})

		task.wait(1)
		new:TweenSize(UDim2.new(0.056, 0, 0.099, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.3, true)
		task.wait(0.3)
		new:TweenPosition(UDim2.new(0.025, 0,0.305, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
		task.wait(0.2)
		new:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.3, true)
		task.wait(0.2)

		new.Parent = nil
	elseif dif - NewValue < 0 then
		print("Lost "..(NewValue - dif)..": Amount of Money")
	end
end)

Yeah that works too you’re right (probably a better practise tbf)

Thanks for your responses guys!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.