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

Switching multiple light colors?

Asked by 4 years ago
Edited 4 years ago

Okay, so I have a model with a bunch of neon parts and I mean a bunch like 300+ and I don't wanna change all their names bc it'll take forever so how could I change all the spotlights colors inside those block, it goes like this: Lights>Neon x300+> Spotlight(Inside every neon block), How can I change every single one of those spotlights color3 property with a switch at once?

0
I'd recommend reading into the use of 'for loops' for changing the light's color. darkelementallord 686 — 4y
0
I've tried using the for i,v in pairs method but it doesnt work because theyre name all the same thing :\ adamson239 12 — 4y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago
local Lights = workspace.Lights:GetChildren()
for _,Neon in pairs(Lights) do
    if (Neon:FindFirstChildOfClass("SpotLight")) then
        Neon.SpotLight.Color = Color3.fromRGB(255,255,255)
    end
end
0
All good but in line 2, You used pairs. You are supposed to use ipairs. Trading_Opportunity 191 — 4y
0
That still doesn't work but no errors are output, I think it's only changing one of the many Neons inside my model, is there any way to make it change every single one of them? adamson239 12 — 4y
0
I can send a screenshot of the explorer if you add me on discord if it helps any Unknown Intelligence#6569 adamson239 12 — 4y
2
@Trading_Opportunity Because this particular task doesn't concern the index value, you can just use the two interchangeably. You'd only really use ipairs when iterating through a dictionary which has its indices as something other than the standard ones that are automatically assigned to values. Rinpix 639 — 4y
View all comments (2 more)
2
Also, this answer has some issues. The L in SpotLight wasn't capitalized, Color3 isn't a property(Color is, Color3 is just the data type), and he put 355(which is probably just a small typo). Don't rush answers; you're prone to make mistakes and it defeats the whole purpose of an answer which is to solve an issue not create more. Rinpix 639 — 4y
0
Apologies;) I didn't rush this, with minimal understanding of his structural hierarchy I had to improvise, I also wrote this very late at night. Ziffixture 6913 — 4y
Ad
Log in to vote
0
Answered by
Rinpix 639 Moderation Voter
4 years ago
Edited 4 years ago

I assume your explorer looks something like this.

Insert a part into the workspace, and insert a ClickDetector inside of that part.

Create a Script inside of ServerScriptService:

local Part = game.Workspace:WaitForChild("Part")
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:Connect(function()
    for i, v in pairs(game.Workspace.Lights:GetChildren()) do
        local R = math.random(255)
        local G = math.random(255)
        local B = math.random(255)
        v.SpotLight.Color = Color3.fromRGB(R, G, B)
        --Variables aren't necessary but I added them to avoid
        --the assignment spanning multiple lines
    end
end)

So, whenever someone clicks on the part you created, it'll loop through the entire model that contains the parts, and on each iteration, it'll change the SpotLight inside of each part so that they are a different color.

In this example, it just randomizes the color, but you can remove the three variables and the assignment of the SpotLight's color if you want to do something else.

0
So I used Faehrens code and fixed a few errors and it worked, but when I tried putting it in my final copy it gave me an error: 20:34:30.283 - Workspace.Map.Alarms1.RaidAlarm.Breach.Buttons.play.Script:8: attempt to index nil with 'GetChildren' I'm not sure how to fix that, if you need to see my explorer or the full code you can shoot me a message on discord bc idk how to post screenshots here adamson239 12 — 4y
0
I would consider getting a better understanding of how the explorer hierarchy works before going any further. https://developer.roblox.com/en-us/resources/studio/Explorer Rinpix 639 — 4y
0
Once you understand the explorer hierarchy and how it works, you should be able to tweak the code so that it fits your criteria. Rinpix 639 — 4y
0
Well I know how it works I just don't understand this error and why it's occuring adamson239 12 — 4y
0
Okay, I fixed the code thank you for your help @RinPix, @Feahren, @darkelementallord adamson239 12 — 4y

Answer this question