Help With Creating A Function To Change Multiple Parts

Hey, so I’m a somewhat beginner at scripting and use help from other sources to script along with my own knowledge, but I need some help with this function. The goal is to get all of the “lights” (really just neon parts) to turn neon blue. The lights are in a folder titled bluelights, to which there are multiple models all holding light parts. I’m calling on this function in a later line of code which works, but the function itself does not work and the output is not helping.

There is another part inside of each model, simply just a part, but that’s why the “if part.Name “lightpart”” then is there.

local function turnonthelights()
	for _, part in bluelights:GetDescendants() do
		if part:IsA("Part") then
			if part.Name "lightpart" then
				part.Color = Color3.fromRGB(0, 255, 255)
			end
			else
				return
			
		end
	end
end

Try removing the else return lines, as I pretty sure those lines are canceling the entire for loop entirely if the part is not named lightpart.

local function turnonthelights()
	for _, part in bluelights:GetDescendants() do
		if part:IsA("Part") then
			if part.Name "lightpart" then
				part.Color = Color3.fromRGB(0, 255, 255)
			end
		end
	end
end

as above don’t need the return and also don’t need to check its class if you have the descendants named lightpart directly

local function turnonthelights()
	for _, part in bluelights:GetDescendants() do
		if part.Name == "lightpart" then  -- should only need this if all the parts to change color are named lightpart and no other descendants
			part.Color = Color3.fromRGB(0, 255, 255)
		end
	end
end

Also try using test prints in your functions and scripts if you run into an issue of it not running correctly this can tell you where it is getting to before stopping and help debug the issue easier

fixed the missing ==

I tried both codes in the replies yet both said in the output that at the line where it talks about finding the name “lightpart” it calls a string value and does not work. I also added a print after the part.color line, and it doesn’t print. The code is in a LocalScript in the StarterPlayerScripts if that makes a difference.

Edit: I also just noticed in your comment that there shouldn’t be any more descendents past the lightpart, but all of those parts do have a pointlight in them, so could that be the problem aswell?

Hi! Code has many problems:

You forgot to add == here
if part.Name == "lightpart" then

As @J_Angry said, else return don’t needed here

And Parts don’t yet loaded on client when script is running.
(Generally parts loading when player’s character appears in game)

So here’s ready script:

if not game:IsLoaded() then
	game.Loaded:Wait()
end

local function turnonthelights()
	for _, part in bluelights:GetDescendants() do
		if part:IsA("Part") then
			if part.Name == "lightpart" then
				part.Color = Color3.fromRGB(0, 255, 255)
			end
		end
	end
end

It’s localscript inside StarterCharacterScripts

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.