Click reverse script

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)
1 Like

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

What does basepart do? i have unions and wedges as well?

Basepart includes all unions, wedges, meshparts, parts hence the name “BaseParts”

1 Like

A BasePart is anything related to a Part I believe. It saves time to use BasePart instead of checking if something is a wedge or a truss etc.

Have a read of this: BasePart | Documentation - Roblox Creator Hub

2 Likes

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

1 Like

Am i just being dumb?

 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

script.Parent.ClickDetector.MouseClick:Connect(lights)

Be sure you set ison to its opposite value after checking

1 Like

I did, but then it turned grey? 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)

Colors turn grey when you input an invalid color, did you check the names of the colors? The w needs to be lowercase on Institutional white

1 Like

You can actually use BrickColor.Red() for red. There’s a few colours implemented in the BrickColor methods.

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.

1 Like

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)
1 Like
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

If you fancy compacting it a bit…

2 Likes

I reccomend @ClockworkSquirrel 's script, it’s compact and very organized which is good to have when you’re scripting.

1 Like

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 :slight_smile:

1 Like

I bet compact scripts like that help save memory in a game and make scripting a lot easier and simpler :slight_smile:

1 Like

Not necessarily saving memory, but does speed up your development time :+1:

2 Likes