1 | for i,v in pairs (game.Workspace.Player [ "Sports Hatchback" ] :GetChildren()) do |
2 | v.light = Instance.new( "PointLight" ) |
3 | light.Parent = game.Workspace.Player.Torso |
4 | light.Range = 200 |
5 | 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:
1 | for i,v in pairs (game.Workspace.Player [ "Sports Hatchback" ] :GetChildren()) do |
2 | if v:IsA( "BasePart" ) then |
3 | local light = Instance.new( "PointLight" ) |
4 | light.Parent = v |
5 | light.Range = 200 |
6 | end |
7 | 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.
01 | local Hatchback = Workspace.Player:FindFirstChild( 'Sports Hatchback' ) |
02 |
03 | local recurse = function (parent) |
04 | for i, child in ipairs (parent:GetChildren()) do |
05 | -- loops through the provided `parent`'s children |
06 | if not (child:IsA( 'Model' )) then |
07 | --direct descendant is not a `Model` |
08 |
09 | light = Instance.new( 'PointLight' ) |
10 | light.Parent = child |
11 | light.Range = 200 |
12 | -- light instanced |
13 | else |
14 | recurse( child ) -- runs the function again on `child ` |
15 | end |
16 | end |
17 | end |
18 |
19 | recurse( Hatchback ) -- call function on model |