I want to achieve it so if someone clicks the button it will turn all the parts red (which it does) however i want it so if it’s click again it’ll revert to institutional white
I’ve tried to add a NOT and a ~= into the the if statement but it didn’t work and i don’t know how i’d go about doing it? If i could get some pointers that be amazing
function lights()
print("debug 1")
local children = workspace.Stage:GetChildren()
local color = BrickColor.new("Institutional white")
print("debug 2")
for i = 1, #children do
local child = children[i]
if child:IsA("Part") then
child.BrickColor = BrickColor.new("Really red")
end
if child:IsA("UnionOperation") then
child.BrickColor = BrickColor.new("Really red")
end
if child:IsA("WedgePart") then
child.BrickColor = BrickColor.new("Really red")
end
end
end
script.Parent.ClickDetector.MouseClick:Connect(lights)
Firstly, you can clean up your code by using the “BasePart” Class instead.
function lights()
print("debug 1")
local children = workspace.Stage:GetChildren()
local color = BrickColor.new("Institutional white")
print("debug 2")
for i = 1, #children do
local child = children[i]
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Really red")
end
end
end
Second, To add the reverse effect, you need to have a condition. Say you’re working with lights you need a variable.
local IsOn = false
function lights()
print("debug 1")
local children = workspace.Stage:GetChildren()
local color = BrickColor.new("Institutional white")
print("debug 2")
for i = 1, #children do
local child = children[i]
if IsOn == false then
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Really red")
end
else
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Institutional White")
end
end
end
end
OMG… How have i not known about this! I am so freaking dumb. Thank you so much! Honestly this website is so helpful at helping me understand and get better
local IsOn = false
function lights()
local children = workspace.Stage:GetChildren()
for i = 1, #children do
local child = children[i]
if IsOn == false then
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Really red")
end
else
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Institutional White")
end
end
end
end
script.Parent.ClickDetector.MouseClick:Connect(lights)
This isn’t working and idk why? It’s supposed to go white if clicked again?
I am really confused as to why it’s not working? No errors and debugs are clear. It goes red just not white
local IsOn = false
function lights()
local children = workspace.Stage:GetChildren()
for i = 1, #children do
local child = children[i]
if IsOn == false then
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Really red")
end
IsOn = true
else
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Institutional white")
end
IsOn = false
end
end
end
script.Parent.ClickDetector.MouseClick:Connect(lights)
Okay so i’ve changed it and now it’s deciding to go mental on me… It all coloured before but now only half is and it’s not changing white if i re click
The entire white bit is supposed to be red then when i click a button it’s supposed to go white but it’s doing random stuff now lmao
local IsOn = false
function lights()
local children = workspace.Stage:GetChildren()
for i = 1, #children do
local child = children[i]
if IsOn == false then
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Really red")
end
IsOn = true
else
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Institutional white")
end
IsOn = false
end
end
end
script.Parent.ClickDetector.MouseClick:Connect(lights)
You need to set it outside of the loop. Right now its turning on off on off every part it loops through. Try checking for the value outside and having 2 loops JUST for setting color inside the two if and else bodies.
That is because, you’re setting IsOn to true while the loop is running. You need to to it outside the loop:
local IsOn = false
function lights()
local children = workspace.Stage:GetChildren()
if IsOn == false then
for i = 1, #children do
local child = children[i]
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Really red")
end
end
IsOn = true
else
for i = 1, #children do
local child = children[i]
if child:IsA("BasePart") then
child.BrickColor = BrickColor.new("Institutional white")
end
end
IsOn = false
end
end
script.Parent.ClickDetector.MouseClick:Connect(lights)
local function lights()
IsOn = not IsOn -- Invert the value of "IsOn"
for _, part in next, workspace.Stage:GetChildren() do
if (part:IsA("BasePart")) then
-- If IsOn == true, set colour to white, otherwise red.
part.BrickColor = BrickColor.new(IsOn and "Institutional white" or "Really red")
end
end
end
I can see how i’ve gone wrong and all these mistakes are valuable, because now i know where i’ve gone wrong and how not to make these mistakes again. I forget i had a loop going due to me getting confused with the colour thing and i see why it wasn’t working, but it’s working now. Thank you everyone for the help. It really does mean a lot