Hello, so pretty much I have a for i, v in pairs looping through a bunch of values and putting it in a table.
The values have attributes which specifies the order it is supposed to go in but when I go through the table, it keeps saying “trying to index string with nil”. At first I didn’t know why, but then I printed the table and then void was randomly found in the table. And every time the server starts up, the position of “void” is different.
My script is down below
function checkstations()
local route = {}
local route_number = string.format("%02i", (Train.CONFIG.Terminus.Value))
local copy_control_unit = game.Workspace.Train_Control.Active_Units:FindFirstChild(Train.Name)
local station_number = copy_control_unit.Route_Info.Next_Stop.Value
for i, v in pairs(game.ReplicatedStorage.RouteTest.Routes:FindFirstChild("Route"..tostring(Train.CONFIG.Route.Value)):GetChildren()) do
local order = v:GetAttribute(route_number.."TOrder")
print(order)
table.insert(route, order, v.Name)
end
print(route)
if copy_control_unit.Route_Info.Next_Stop.Value ~= 0 then
print(station_number)
MainUI.Main_Operations.Main.Station.Next.StationName.Text = route[station_number]
MainUI.Main_Operations.Main.Station.NextNext.StationName.Text = route[station_number + 1]
end
end
game.Workspace.Train_Control.Active_Units:FindFirstChild(Train.Name).Route_Info.Next_Stop.Changed:Connect(checkstations)
I don’t know how to fix this or to get rid of it, so help would be appreciated.
I tried doing “if == nil then return end” but it still happens and there are barely any devforums with good solutions or explanations on why this happens.
That’s a different problem from what you said here though
Very different problems. What you just said makes more sense and I can take a guess what line the error is on now.
If I were to chance a guess, it’s probably because of table.insert shenanigans. The table is being filled based on the route_number.."TOrder" attribute. Can I assume that you’re not missing any attributes and that they’re all consecutive integers from 1 to n? If so, I think I might have a solution.
Yeah I’m not fully certain why you’re getting void, but I suspect it’s somewhat because table.insert expects inserted values to be located within the range of the table. So if stop #8 was inserted when the routes table was only filled up to stop #4, it would fail. Still have no idea why you’d get nil values like that though, I would imagine that table.insert would either do nothing (in which case your table would end at the first nil value) or it would add it to the end of the table (in which case your table should be out of order)
No clue why it’s screwing up in this particular way.