Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

an error occurs with GetChildren()?

local chunkpieces = script.Parent.ChunkPieces:GetChildren("ChunkPiece")
wait(0.5)--wait time after chunk is loaded to reduce lag
for i = 1,25 do
	wait(0.1)
	if chunkpieces[i]:IsA("Model") and chunkpieces[i].Name == "ChunkPiece" then
		b = math.random(#chunkpieces)
		local randompart = chunkpieces[b]
		randompart.IsEnabled.Value = true
	end
end

this script goes through a model which contains lots of other models, each of those other models is a group of parts. an error occurs and the for loop only runs once.

the error:

Workspace.AreaChunk3.CoreManager:6: attempt to index field '?' (a nil value)

1 comment

0

GetChildren() is a table not a spcific target, it gets all the children

2 answers

local chunkpieces = script.Parent.ChunkPieces:GetChildren()--get children is a table and can not find a specific child
wait(0.5)--wait time after chunk is loaded to reduce lag
for i = 1,25 do
    wait(0.1)
    if chunkpieces[i]:IsA("Model") and chunkpieces[i].Name == "ChunkPiece" then
        b = math.random(#chunkpieces)
        local randompart = chunkpieces[b]
        randompart.IsEnabled.Value = true
    end
end

3 comments

0

the same error comes up still.

0

nvm i know what happened, the arrangment of models got a bit mixed up and its looking for the wrong thing :/

0

and for the mod who posted a note on my answer, i did the explanation as a comment on his question, no need to post a note

Shouldn't it be:

b = math.random(1,#chunkpieces)

Also looping could be done by usi g #chunkpieces instead of 25

This discussion is preserved as it was. New answers, comments, and votes are closed.