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

How to put a pointlight on everything inside a model?

Asked by
h19rry 20
10 years ago
1for i,v in pairs(game.Workspace.Player["Sports Hatchback"]:GetChildren()) do
2 v.light = Instance.new("PointLight")
3light.Parent = game.Workspace.Player.Torso
4light.Range = 200
5end

Thanks in advance.

2 answers

Log in to vote
1
Answered by
Wizzy011 245 Moderation Voter
10 years ago

Firstly, v.lightwould mean that there is currently an instance called "light" in the object, you should just be defining a variable using local light

In a forloop, 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:

1for 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
7end

Hope this helps!

0
That fixed it, thankyou. h19rry 20 — 10y
Ad
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
10 years ago

Adding on to Wizzy011's answer..

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.

01local Hatchback = Workspace.Player:FindFirstChild('Sports Hatchback')
02 
03local 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
17end
18 
19recurse( Hatchback ) -- call function on model
1
^ (I didn't want to overcomplicate the matter further :P) Wizzy011 245 — 10y
0
I already typed it up when i saw you answered so i added the first line :P ImageLabel 1541 — 10y

Answer this question