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
9 years ago
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.

2 answers

Log in to vote
1
Answered by
Wizzy011 245 Moderation Voter
9 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:

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!

0
That fixed it, thankyou. h19rry 20 — 9y
Ad
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 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.

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
1
^ (I didn't want to overcomplicate the matter further :P) Wizzy011 245 — 9y
0
I already typed it up when i saw you answered so i added the first line :P ImageLabel 1541 — 9y

Answer this question