Attempt to index nil with number?

so im making a td game, im trying to revamp the upgrade system because the old one sucked, but i keep getting the error:

11:58:54.100  Players.userni3.PlayerGui.StatsGui.LocalScript:31: attempt to index nil with number  -  Client - LocalScript:31
  11:58:54.100  Stack Begin  -  Studio
  11:58:54.100  Script 'Players.userni3.PlayerGui.StatsGui.LocalScript', Line 31  -  Studio - LocalScript:31
  11:58:54.100  Stack End  -  Studio

this is my code:

	script.Parent.StatsFrame.Upgrade.Text = "$".. towercosts[towername][currentmodel.NextLevel.Value]
	script.Parent.StatsFrame.Upgrade.MouseButton1Down:Connect(function()	
		if game.Players.LocalPlayer.stats.Cash.Value >= towercosts.Data[towername][currentmodel.NextLevel.Value] then
		currentmodel = remote3:InvokeServer(towername)
		local newmodel = remote2:InvokeServer(currentmodel,currentmodel.NextLevel.Value,currentmodel.HumanoidRootPart.Position,towername,currentmodel.Parent)		
		script.Parent.StatsFrame.Upgrade.Text = "$".. towercosts.Data[towername][currentmodel.NextLevel.Value]
		local newRangeViewSize = game.ReplicatedStorage.GetTowerRange:InvokeServer(currentmodel)
		rangeview.Size = newRangeViewSize
		gui.StatsFrame.Range.Text = "Range: "..currentmodel.Configuration.Range.Value
		gui.StatsFrame.Damage.Text = "Damage: "..currentmodel.Configuration.Damage.Value
			gui.StatsFrame.Firerate.Text = "Firerate: "..currentmodel.Configuration.Firerate.Value
			end
	end)
end)

its messy, i know
currentmodel is the towers current model
the currentmodel is a returned value by the server, it does have nextvalue, i dont know why it errroed

What line in the script does the error refer to?

line 31,

which is this

script.Parent.StatsFrame.Upgrade.Text = "$".. towercosts[towername][currentmodel.NextLevel.Value]

You haven’t given us much information on the variables like towercosts.

It’s probably happening because currentmodel might be set to nil

When it says “trying to index nil with a number” what that means is that the table you are trying to index using square brackets is nil. So, in this case, the only table in that line is towercosts. Either that variable is misspelled (could be case) or it is a proper reference, but is “nil” or “void”.

towercosts is a module that contains how much the upgrades for the towers cost

currentmodel is a instance returned by the server

1 Like

try doing

if currentmodel ~= nil then
   -- some function
end

it did run the code, but why is the currentmodel nil?

A module is just a table. Anything assigned to “{}” is a table. Take a look at the module. Are you returning the module table at the bottom of it? If not, the require statement will assign nil to towercosts.

1 Like

it does return the module table

Okay, so the code in the first section shows towercosts[towername] and towercosts.Data[towername] are both of those expected to be defined, or should the first one also use “.Data”? Also, if you can print what “towername” resolves to, then you would expect to see either a function or property in the module with the same name as the value stored in “towername”.

i put .Data infront of it, now it errors:

  12:31:16.616  Players.userni3.PlayerGui.StatsGui.LocalScript:34: attempt to concatenate string with nil  -  Client - LocalScript:34
  12:31:16.616  Stack Begin  -  Studio
  12:31:16.616  Script 'Players.userni3.PlayerGui.StatsGui.LocalScript', Line 34  -  Studio - LocalScript:34
  12:31:16.616  Stack End  -  Studio

Okay, so that fixed the first issue you had. Now it is saying it can’t concatenate a string with nil. What that means is that the string “$” cannot be combined with an expression that results in nil. So, the “towercosts.Data” reference is nil.

You need to print what “towername” and “currentmodel.NextLevel.Value” resolve to and then see if these are defined in the towercosts module. Let’s say they resolve as towername=“firstTower” and nextlevel=“2”. Then you would expect to see something like this in your towercosts module…

module = {
Data = {
firstTower = { 10, 20, 30 ,40}
}
}

The value `module.Data["firstTower"][2]` would be the number 20.  To make that a string you would wrap it with`tostring()`.

this is what one of the tables in the module looks like

["Gunner"] = {
	["Cost"] = 250,
	["1"] = 50,
	["2"] = 100,
	["3"] = 250
},

Okay, so the way you would reference the cost is:

module.Gunner.Cost
-- or
module["Gunner"]["Cost"]

Also, “currentmodel.NextLevel” must be a string value, otherwise you need to convert it to a string because all of the key values in your module appear to be defined as strings which is why they are in square brackets with quotes.

i reference it like:

module.Gunner.Cost