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

How do i make a LightPoint clone to every parts in another model?

Please help me, i want to make a LightPoint clone to every parts in another model, but only one time :C

Current script:

if script.Parent.items:GetChildren().Name == "Cube" then
	local light = Instance.new("PointLight")
	light.Brightness = 2
	light.Range = 6
	light.Shadows = true
	light.Enabled = true
	if light.Parent.BrickColor == "Br. yellowish green" then
	  light.Color = Color3.new["Br. yellowish green"]
	end
	if light.Parent.BrickColor == "Neon orange" then
	  light.Color = Color3.new["Neon orange"]
	end
	if light.Parent.BrickColor == "Bright orange" then
	  light.Color = Color3.new["Bright orange"]
	end
	if light.Parent.BrickColor == "Institutional white" then
	  light.Color = Color3.new["Institutional white"]
	end
	wait(0.1)
	light:clone().Parent = script.Parent.items:GetChildren()
end

It's not cloning.

1 answer

✓ Accepted answer

Lets take a look at this piece by piece. First line:

if script.Parent.items:GetChildren().Name == "Cube" then

To start with, make sure you are familiar with what the GetChildren() function actually does. It returns a table, not a Part! A table doesn't have a Name property, so you can't check if the Name of a table is "Cube".

Now let's look at the next bit:

    if light.Parent.BrickColor == "Br. yellowish green" then
      light.Color = Color3.new["Br. yellowish green"]
    end
    if light.Parent.BrickColor == "Neon orange" then
      light.Color = Color3.new["Neon orange"]
    end
    if light.Parent.BrickColor == "Bright orange" then
      light.Color = Color3.new["Bright orange"]
    end
    if light.Parent.BrickColor == "Institutional white" then
      light.Color = Color3.new["Institutional white"]
    end

You don't need to use if statements here! The above can be simplified to:

light.Color = light.Parent.BrickColor.Color --Sets the light's color to the Color3 equivalent of the BrickColor of the Parent

The next bit:

light:clone().Parent = script.Parent.items:GetChildren()

Again, GetChildren() returns a table, not a Part (or each Part one-by-one, for that matter).

I'll write a script that does a similar thing for you and explain it along the way. First let's start by making the PointLight and setting it's properties, which you do very well so I'll just copy it:

light = Instance.new("PointLight")
light.Brightness = 2
light.Range = 6
light.Shadows = true
light.Enabled = true

The next step is very important to becoming a proficient scripter, it's called Iteration. We are going to iterate through each element of the table returned by the GetChildren() method, this just means that we're going to go through each part one-by-one.

for _,part in pairs(script.Parent.items:GetChildren())
	if part.Name == "Cube" then
		newLight = light:clone() --Cloning the light that we made above
		newLight.Color = part.BrickColor.Color --Set the color to the part that it's being put in
		newLight.Parent = part --Parent this new light to this part
	end
end

Also, don't forget that Lua (and most other programming languages) are case-sensitive, if you don't use the right capitalisation for the names of things that you have created, there will be an error.

Make sure that, when you run the script, you have View > Output Window enabled so that you can see if any errors come up.

Here is the final script:

--Create the Light
light = Instance.new("PointLight")
light.Brightness = 2
light.Range = 6
light.Shadows = true
light.Enabled = true

--Iterate through the Children and add Colored lights to them
for _,part in pairs(script.Parent.items:GetChildren())
	if part.Name == "Cube" then
		newLight = light:clone() --Cloning the light that we made above
		newLight.Color = part.BrickColor.Color --Set the color to the part that it's being put in
		newLight.Parent = part --Parent this new light to this part
	end
end

I hope that helps!

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