Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to put a pointlight on everything inside a model?

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

✓ Accepted answer

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 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!

1 comment

0

That fixed it, thankyou.

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

2 comments

1

^ (I didn't want to overcomplicate the matter further :P)

0

I already typed it up when i saw you answered so i added the first line :P

This discussion is preserved as it was. New answers, comments, and votes are closed.