hello, i got a problem with my game throwing an error that im trying to index a nil value while doing a GetChildren() function but ive used it before in almost the same way and ive checked the index multiple times and it is doing it at the right place. heres the script, error is at line 35. it is a script ran on the server.

Error message: 21:16:43.539 - Workspace.AhmedCountry.SouthEastState.UpdateRoadModel:35: attempt to index a nil value

InfraLevel = game.ServerStorage.CountryData:FindFirstChild(script.Parent.Parent.Name).StateData:FindFirstChild(script.Parent.Name).InfrastructureStateLevel

function ChooseRoad()
	if InfraLevel.Value == 1 then
		Clear()
		Road("DirtRoad", 0)
	elseif InfraLevel.Value == 2 then
		Clear()
		Road("DirtRoad2", 0)
	elseif InfraLevel.Value == 3 then
		Clear()
		Road("Road", 0)
	elseif InfraLevel.Value == 4 then
		Clear()
		Road("Road2", 0)
	elseif InfraLevel.Value == 5 then
		Clear()
		Road("GoodRoad", 0)
	elseif InfraLevel.Value == 6 then
		Clear()
		Road("GoodRoad2", 0)
	end
end

function Road(road, request)
	local AllParts = script.Parent.Infrastructure:FindFirstChild(road):GetChildren()
		for i = 1, #AllParts do
			script.Parent.Infrastructure:FindFirstChild(road):FindFirstChild(AllParts[i]).Transparency = request
		end	
end

function Clear()
	local road = script.Parent.Infrastructure:GetChildren()
		for o = 1, #road do
			local AllParts = script.Parent.Infrastructure:FindFirstChild(road[o]):GetChildren()
			for i = 1, #AllParts do
				script.Parent.Infrastructure:FindFirstChild(road[o]):FindFirstChild(AllParts[i]).Transparency = 0
			end	
	end
end

ChooseRoad()

InfraLevel.Changed:Connect(ChooseRoad)

2 comments

0

So for future reference when you get to a problem like this, the game is trying to use the children of the an Instance and there are no children. So a good way to figure out where something is nil is to put a print statement in the for loop that prints the index. That way you know exactly where the error occurs.

0

Also you could say for index, values (road) And that accomplishes the same thing, but instead of the indexes being numbers it will be the names of whatever the children are of road.