for i,v in pairs(game.Workspace.Player["Sports Hatchback"]:GetChildren()) do v.light = Instance.new("PointLight") light.Parent = game.Workspace.Player.Torso light.Range = 200 end
Thanks in advance.
Firstly, v.light
would mean that there is currently an instance called "light" in the object, you should just be defining a variable using local light
In a for
loop, the letter i stands for the current number of instances your loop has looked through currently.
The v in turn is the actual instance itself, so to add a light to each part.
Therefore change** line 3** to light.Parent = v
, which would add a light to each part in the model!
Just to be more careful, I'd add an if statement into the for loop just to make sure the parts you're working on are definitely parts! You can do this with:
for i,v in pairs(game.Workspace.Player["Sports Hatchback"]:GetChildren()) do if v:IsA("BasePart") then local light = Instance.new("PointLight") light.Parent = v light.Range = 200 end end
Hope this helps!
You could use a simple loop to iterate through every child of a certain model, but for models within the model, you would have to use recursion
. Recursion allows for you to use a function in case the descendants of the model aren't all direct descendants.
local Hatchback = Workspace.Player:FindFirstChild('Sports Hatchback') local recurse = function (parent) for i, child in ipairs(parent:GetChildren()) do -- loops through the provided `parent`'s children if not (child:IsA('Model')) then --direct descendant is not a `Model` light = Instance.new('PointLight') light.Parent = child light.Range = 200 -- light instanced else recurse( child ) -- runs the function again on `child ` end end end recurse( Hatchback ) -- call function on model