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

Colour changing light with button not working?

Asked by 4 years ago

3 buttons, 1 Red, 1 Yellow, 1 Green 5 Lights When I press the Red button I need all the lights to turn red When I press the Yellow all the lights to turn Yellow And when I press the green button I need all lights to turn green

This is the script I came up with for the green one, nothing happens when I press the green button. I'm pretty new to scripting but there doesn't seem to be any tutorials or even free models that are similar.

local Lights = script.Parent.Lights

-- Light Posts
local A  = Lights.A
local B  = Lights.B
local C  = Lights.C
local D  = Lights.D
local E  = Lights.E


function changeColour()
    A.Light.BrickColor = BrickColor.Green()
    B.Light.BrickColor = BrickColor.Green()
    C.Light.BrickColor = BrickColor.Green()
    D.Light.BrickColor = BrickColor.Green()
    E.Light.BrickColor = BrickColor.Green()

end


function onClick()
    changeColour()

end

script.Parent.ClickDetector.MouseClick:Connect(onClick)
0
Do you already have a light inside of the parts? What type of light is it Surface, Point? Nefarioum 162 — 4y
0
No, there are no lights. The brick is Neon and it's more the colour changing than actually providing a light source. naplank 9 — 4y
0
^They are Safety lights for a Go-Karting track naplank 9 — 4y
0
Is "Light" the name of the part inside the light post you want to change color? If so, then that's good. Try changing Light.BrickColor = BrickColor.Green() to Light.BrickColor = BrickColor.new("Green") GuruNin 5 — 4y

1 answer

Log in to vote
0
Answered by
Nefarioum 162
4 years ago

So from what I understand - you do not have any lights within your parts and you just want to change the colour of a part. Within a part itself, it does not have any properties called "Light" so in order to achieve changing its colour you would remove the .Light from lines 12 - 16. This would change all of the bricks to the colour green.

However, there are multiple other ways you can fine tune down your script to cut lines and make it more efficient and I'll walk you through how to do this. To begin, we will be directly running the function from the event. This saves the need to create a function:

script.Parent.ClickDetector.MouseClick:Connect(function ()
 -- Code here
end)

Now, whenever the button is clicked, it will run the code inside of those two lines. This already cuts down 6 of lines of code. We will now need to go through all of the parts to make alterations to them but we do not want to specify each parts. This takes times and isn't practical if you had decided to add multiple lights later down the line so we will be using array iteration to solve this.

script.Parent.ClickDetector.MouseClick:Connect(function ()
    for _, Value in pairs(script.Parent.Lights:GetChildren()) do -- This goes through all of the children of Lights
        -- Code here
    end
end)

Now, whenever the button is clicked, it will run through all of the children of lights. This has saved us having to define all of them. All that is left to do is to assign a colour to the parts when it is clicked. We will be used BrickColor.new("") to achieve this. The final completed code can be viewed below.

Hope this helped!

script.Parent.ClickDetector.MouseClick:Connect(function ()
    for _, Value in pairs(script.Parent.Lights:GetChildren()) do -- This goes through all of the children of Lights
        Value.BrickColor = BrickColor.new("Bright green") -- This will turn them all to bright green
    end
end)
Ad

Answer this question