Attempt to compare nil <= number

im making a tower defense game, but i keep getting this error:

attempt to compare <= nil with number

this is the code:

function refresh()
	print(currentmodel.NextLevel.Value)
	print(ctower.Costs[currentmodel.Parent.Name][currentmodel.NextLevel.Value])
	dmg.Text = "Damage: ".. currentmodel.Configuration.Damage.Value
	range.Text = "Range: ".. currentmodel.Configuration.Range.Value
	firerate.Text = "Firerate: ".. currentmodel.Configuration.Firerate.Value
	upgrade.Text = "$"..tostring(ctower.Costs[currentmodel.Parent.Name][currentmodel.NextLevel.Value])
end

function Upgrade() 
	if currentmodel.Level.Value ~= currentmodel.MaxLevel.Value and plr.Stats.Cash.Value >= tonumber(ctower.Costs[currentmodel.Parent.Name][currentmodel.NextLevel.Value]) then
		currentmodel = serverUpgrade:InvokeServer(currentmodel)
		
		refresh()
		
	    local newRVSize = getTowerRange:InvokeServer(currentmodel)
		rangeview.Size = newRVSize
	else
		upgrade.Text = "Maxxed!"
	end
end

also this isnt the full script, currentmodel is a value which holds the towers model, ctower is a module with all the information about towers plr is the localplayer.

tonumber(ctower.Costs[currentmodel.Parent.Name][currentmodel.NextLevel.Value])

This is definitely the cause of the error. Make sure the references are correct.

tonumber will return a nil if it gets anything other than numbers.

i removed tonumber, still errored

it would still error since you’re doing >= or greater than or equals to which is for number only, and it contains a string that’s why it was making errors at the first place, try printing

this

it printed this:

nil

extracharactersforthe30charactercap

Yeah well then one of the two values you’re comparing is nil, you need to go back a few steps and look at the origins of both values, see if both of them are not nil and if so make em a valid value to compare

Print these:
ctower
ctower.Costs
ctower.Costs[currentmodel.Parent.Name]
plr.Stats.Cash.Value

Just don’t have convoluted instance trees, it’s unnecessary and slow.

okay, so none of them printed nil, which is good, but why does it print nil when i add [currentmodel.NextLevel.Value]?

i dont even know what a convulted instance tree is, im not the best at tables

nevermind, fixed it myself, im dumb

i had to use tostring() because when i put ["1"] it printed 50, which was the price for the upgrade
and i was using currentmodel.NextLevel.Value which is a NUMBER, the table requires a string, not a number.

im a idiot, sorry for wasting your time