Void keeps popping up in the table

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 strikes me as a somewhat odd error to have. Which line is the error message coming from?

The “void” thing doesn’t pop up as an error, but when I try read the table.

It says “Unable to assign property Text. string expected, got nil”

And what’s also weird is that in every server, it’s a bit different.

Also, the table looks like this
image
The order of the table is correct, but void is placed randomly in between, it’s very weird.

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 the numbers are perfectly going up in order, this is also proved because “void” is placed randomly in every server.

1 Like

What happens if you replace
table.insert(route, order, v.Name)
with
route[order] = v.Name

1 Like

Wow that works, I wonder why I didn’t think of it sooner, thanks for the solution though.

But do you have any idea why this happens or what it is?

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.