Currency Counter

Hello my Friends,
I actually use a script to count the “Clicks” i make when i click on the Screen and the Counted Number is showed on the Screen for a Second. Now i have the Problem that when you higher your Clicks per second to like 10CPS but then lower it to like 5CPS the Number is not longer showed on the Screen and this pops up on the console:

Players.MeloneHD98.PlayerGui.ClicksPop:11: attempt to compare nil < number

Here is the Script i use, can somebody help me?

wait();
local u1 = { "", "K", "M", "B", "T", "Qd", "Qn", "sx", "Sp", "O", "N", "D", "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 v1(p1)
	for v2 = 1, #u1 do
		if tonumber(p1) < 10 ^ (v2 * 3) then
			return math.floor(p1 / (10 ^ ((v2 - 1) * 3) / 100)) / 100 .. u1[v2];
		end;
	end;
end;

local plr = game.Players.LocalPlayer
local ms = plr:GetMouse()
local stat = plr:WaitForChild("leaderstats")["Clicks"]
local val = plr:WaitForChild("leaderstats")["Clicks"].Value

stat.Changed:Connect(function()
	wait(0.1)
	local text = script.Parent.SoulsScreen.TextLabel:Clone()
	text.Parent = script.Parent.SoulsScreen
	text.Name = "+".. tostring(stat.Value-val)
	text.Text = v1("+".. tostring(stat.Value-val))
	text.Rotation = math.random(-20, 20)
	text.Visible = true
	local pos = UDim2.new(math.random(0,1000)/1000, 0,0.924, 0)
	text.Position = pos
	text:TweenPosition(pos-UDim2.new(0,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,1)
	wait(1)
	val = stat.Value
	text:Destroy()
end)

The error message you are seeing means that the script is trying to compare a “nil” value with a number, which is not allowed. In this case, the problem might be caused by the “v1” function returning a nil value, which is then compared with a number in the line that is causing the error.

To fix this, you can add a check in the “v1” function to make sure it always returns a value. One way to do this is to add a default case at the end of the function that returns the original number as a string. Here’s an updated version of the “v1” function with this change:

local function v1(p1)
	for v2 = 1, #u1 do
		if tonumber(p1) < 10 ^ (v2 * 3) then
			return math.floor(p1 / (10 ^ ((v2 - 1) * 3) / 100)) / 100 .. u1[v2];
		end;
	end;
	-- Default case: return the original number as a string
	return tostring(p1)
end;

This should ensure that the “v1” function always returns a value, even if the input is larger than the largest unit in the “u1” table.

Additionally, to prevent the error message from appearing when the clicks per second drops below a certain threshold, you can add a check in the “stat.Changed” event to make sure the value of “stat.Value” is not nil before updating the counter. Here’s an updated version of that part of the code:

stat.Changed:Connect(function()
	wait(0.1)
	if stat.Value ~= nil then -- Check if the value is not nil
		local text = script.Parent.SoulsScreen.TextLabel:Clone()
		text.Parent = script.Parent.SoulsScreen
		text.Name = "+".. tostring(stat.Value-val)
		text.Text = v1("+".. tostring(stat.Value-val))
		text.Rotation = math.random(-20, 20)
		text.Visible = true
		local pos = UDim2.new(math.random(0,1000)/1000, 0,0.924, 0)
		text.Position = pos
		text:TweenPosition(pos-UDim2.new(0,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,1)
		wait(1)
		val = stat.Value
		text:Destroy()
	end
end)

With these changes, the script should be able to handle changes in clicks per second without throwing any errors.

There is still the same error but just for like 10 Seconds and then it works again

This won’t solve the topic, I just wanted to mention that I have made a number-formatting function as well, however, mine gets rid of the loop(instead of checking each currency and comparing, it calculates the power using logarithms), thought to drop it here in case you find it useful: