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

GetChildren() not working? [ANSWERED]

Asked by 8 years ago

Not to sure whats happening with my code I've tried using a variable and that too didn't work. Its a season changer that changes the mesh's texture id to make the mesh appear different for a certain season. Heres my code:

if script.Summer.Value == true then
    script.Parent:GetChildren().Mesh.TextureId = "http://www.roblox.com/asset/?id=32323659"
elseif script.Fall.Value == true then
    script.Parent:GetChildren().Mesh.TextureId = "http://www.roblox.com/asset/?id=100779383"
elseif script.Winter.Value == true then
    script.Parent:GetChildren().Mesh.TextureId = "http://www.roblox.com/asset/?id=65612688"
end

I have 3 Bool Values inside the script named Summer,Fall, and Winter

I'm getting the error: Workspace.Trees.Script:6: attempt to index field 'Mesh' (a nil value)

Please help

2 answers

Log in to vote
4
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Problem

This is a common misconception with the GetChildren() function as many users assume that by using the function it will automatically change all values of the area GetChildren was called on. This however is not the case as the GetChildren() function always returns a table value.


Solution

In order to use the GetChildren() function to change all values of a model or group of objects you will need to use a for loop. You will want to establish for i,v in pairs() do. The i and v basically stand for index and value as the "in pairs" means that for every index and value together do something in the script. "v" in this case is the object that is getting the value changed.


Final Script

However, due to the likelihood that the script will not have a Mesh object inside it, I would recommend naming the model something unique and moving the script out of the model.

if script.Summer.Value == true then
    for i,v in pairs(script.Parent:GetChildren()) do
        v.Mesh.TextureId = 'http://www.roblox.com/asset/?id=32323659'
    end
elseif script.Fall.Value == true then
    for i,v in pairs(script.Parent:GetChildren()) do
        v.Mesh.TextureId = 'http://www.roblox.com/asset/?id=100779383'
    end
elseif script.Winter.Value == true then
    for i,v in pairs(script.Parent:GetChildren()) do
        v.Mesh.TextureId = 'http://www.roblox.com/asset/?id=65612688'
    end
end

Hopefully this helped, if so hit the upvote button. If this answered your question please hit accept answer! If you have any questions, then please comment below.
Ad
Log in to vote
1
Answered by 8 years ago

Your problem:

GetChildren() returns a table of objects that are the children, and you cannot access all of them by finding a child.

Do this instead:

for i,v in pairs (script.Parent:GetChildren()) do -- for loop that does the script for every child
    v.Name  = "part" ..i
end

Answer this question