Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

an error occurs with GetChildren()?

Asked by 4 years ago
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)

0
GetChildren() is a table not a spcific target, it gets all the children Gameplayer365247v2 1055 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
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
0
the same error comes up still. mantorok4866 201 — 4y
0
nvm i know what happened, the arrangment of models got a bit mixed up and its looking for the wrong thing :/ mantorok4866 201 — 4y
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 Gameplayer365247v2 1055 — 4y
Ad
Log in to vote
0
Answered by
karlo_tr10 1233 Moderation Voter
4 years ago

Shouldn't it be:

b = math.random(1,#chunkpieces)

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

Answer this question