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
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”.
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.
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”.
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…
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.