Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

(Probably easy fix) Attempt to compare number to nil?

This is probably an easy fix, but I keep getting this error 'Attempt to compare number to nil' when obviously i'm NOT comparing anything to 'nil'.

local module = {}

function module:abbreviate(numz)
	local num = tonumber(numz)
	if num >= 1 and num <= 999 then
		return "$"..tostring(num)
	end
end

return module

other script:

module = require(Settings)
game.Players.PlayerAdded:connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = 'leaderstats'
	local rm = Instance.new("NumberValue", game.Lighting.MoneyStorage)
	rm.Name = plr.Name
	rm.Value = 0	
	local money = Instance.new("StringValue", leaderstats)
	money.Name = "Cash"
	money.Value = "$"..tostring(rm.Value)
	
	rm.Changed:connect(function()
		local mtp = rm.Value
		local mon = module.abbreviate(mtp)
		money.Value = mon
	end)
end)

4 comments

0

What line is the error coming from?

0

Try changing module:aberviate() to module.abreviate() in the first script.

0

You need to remove the '$' from mtp in Line 13. Try using: local mtp = string.gsub(rm.Value,"$","")

0

I figured this before you guys even asked, but the problem was that I put : before instead of a . on line 3 in the modulescript

1 answer

✓ Accepted answer

Methods

  • Methods must be treated as methods
    Because you're declaring the function as module:abbreviate, you really must call it as module:abbreviate unless you want to have unexpected results. Methods are syntax sugar for calling the function with the container as the first argument, which means that until you understand what they're hiding you should just stick to doing stuff the normal way.

  • tonumber will return nil if the argument is not a number
    Your value num comes from tonumber, which is currently not having its output validated. Because you're not treating the function as a method, numz is nil, and therefore not in an appropriate format for numbers - Causing tonumber to return nil as a result.

1 comment

0

thanks I guess, even though I figured it out before this answer

This discussion is preserved as it was. New answers, comments, and votes are closed.