Attempt to compare number and nil

so im making a td game, im rewritting the upgrading system because the old one sucks, but i keep getting the error:

  14:53:29.197  ServerScriptService.ServerTowers:33: attempt to compare string <= number  -  Server - ServerTowers:33
  14:53:29.197  Stack Begin  -  Studio
  14:53:29.198  Script 'ServerScriptService.ServerTowers', Line 33  -  Studio - ServerTowers:33
  14:53:29.198  Stack End  -  Studio

this is the code:

event3.OnServerInvoke = function(player,tower,level,towerpos,towername,towerpart)
	if player.stats.Cash.Value >= tostring(towercosts.Data[towername][level]) then
		local oldtower = tower
		local newtower = towermodels[towername][level]:Clone()
		newtower.Parent = towerpart
		newtower:MoveTo(towerpos)
		for i,v in pairs(tower:GetDescendants()) do
			if v:IsA("Part") or v:IsA("MeshPart") then
				v.Anchored = true
				v.CanCollide = false
				v.Transparency = 1
			end
		end

		newtower.Name = "TowerModel"
		oldtower.Name = "OldTowerModel"..math.random(1,1000)
		oldtower.Parent = workspace.OldTowerModels
		newtower.TowerGeneral.Disabled = false
		newtower.Script.Disabled = false
		newtower.TowerGeneral.GuiDetector.Disabled = false
		if oldtower:FindFirstChild("Humanoid") then oldtower.Humanoid:Destroy() end
		for i,v in pairs(oldtower:GetDescendants()) do
			if v:IsA("Script") then
				v.Disabled = true
			end
		end
		player.stats.Cash.Value -= towercosts.Data[towername][tostring(level)]
		oldtower:Destroy()
		return newtower
	else
		game.ReplicatedStorage.Notify:FireClient(player,"You don't have enough cash to upgrade that!",4)
	end
end

and this is the line that is throwing the error:

if player.stats.Cash.Value >= tostring(towercosts.Data[towername][level]) then

towercosts is a module that contains how much towers cost to upgrande and place, towername is the name of the tower, and level is the level its trying to upgrade to

You simply cannot compare a string or a number together like you are doing.

Instead of using tostring(), maybe use tonumber()

title is misleading, you said number and nil not number and string

anyways try these

player.stats.Cash.Value >= towercosts.Data[towername][level]

or

player.stats.Cash.Value >= tonumber(towercosts.Data[towername][level])

By the looks of it it’s due to your use of tostring which converts the level into a string. You’re trying to compare the cash value which is a number to a string.

i replaced tostring with tonumber, still didnt do anything

i removed tonumber, nothing happened

Did you get a different error or the same error?

i still got the same error

(extrastuffherefor30chars)

Hi, first post so hope I get this right.:crossed_fingers:

I think your problem is one of “Programmer’s Eye” :eyes:. It’s sometimes hard to really see your own work (which is why most pro companys use pair programming). You often only see what you think should be there, or what you want to be there. Need to teach yourself to read code like you didn’t write it, which is hard.

if player.stats.Cash.Value >= tostring(towercosts.Data[towername][level]) then

but later on…

player.stats.Cash.Value -= towercosts.Data[towername][tostring(level)]

Do you see it now? :cool: