Button not making the part show up again after being pressed

Hello developers! I got a problem. I’ve been trying to make a button that makes a part invisible, turns canCollide off and changes color. Later, after pressing it again, I want it to make the part visible again, turn on canCollide and change the color once again. I also want to make sure that the button can be pressed only every 2 seconds. Can anyone help me?

Here is the script:

local ClickyDetector = script.Parent.ClickDetector
local InvisibleStopper1 = game.Workspace.InvisibleStoppers.InvisibleStopper1
local debounce = false

ClickyDetector.MouseClick:Connect(function()
	
		if script.Parent.Color == Color3.fromRGB(255, 0, 0) and debounce == false then
			debounce = true
			print("succesfully registered click")
			InvisibleStopper1.Transparency = 1
			InvisibleStopper1.CanCollide = false
			InvisibleStopper1.CastShadow = false
			script.Parent.Color = Color3.new(0.0784314, 0.619608, 0.0666667)
			task.wait(2)
			debounce = false
		
	elseif script.Parent.Color == Color3.fromRGB(0.0784314, 0.619608, 0.0666667) and debounce == false then
			debounce = true
			print("succesfully registered click")
			InvisibleStopper1.Transparency = 0.75
			InvisibleStopper1.CanCollide = true
			InvisibleStopper1.CastShadow = true
			script.Parent.Color = Color3.new(255, 0, 0)
			task.wait(2)
			debounce = false
		end
	
end)

Workspace:
image

3 Likes

In your 2nd if statement condition, you seem to be trying to use a normalised (0-1) colour value in Color3.fromRGB, which only takes integers in the range 0-255.
You can fix this by changing it to Color3.new instead.
I’d suggest using fromRGB across all uses of colours, though.

Also, just a tip, you can shorten your if statement conditions by having an “early return” at the very start of the code:

ClickyDetector.MouseClick:Connect(function()
	if debounce then return end
	if script.Parent.Color == Color3.fromRGB(255, 0, 0) then
		debounce = true
		...
	elseif script.Parent.Color == Color3.new(0.0784314, 0.619608, 0.0666667) then
		debounce = true
		...
	end
end)
3 Likes

Color3.new is mostly good for things like scripting a color randomizer & other.

but mainly Color3.FromRGB is most recommendable.

2 Likes

It works! Oh and also, why are you using return? Couldn’t you use break or is it inefficient or something?

1 Like

break is only used in loops. return just ends the current function you’re in, and returns whatever you give it to the code that called the function.

2 Likes

break is for loops.

return is to return things, you can also return nil by doing that so
like that if debounce then return end it returns nil and does nothing. its good to make the code readable

it ends the function, like breaks the function if its what you like to call

1 Like

So my full script should look something like this right?

script:

local ClickyDetector = script.Parent.ClickDetector
local InvisibleStopper1 = game.Workspace.InvisibleStoppers.InvisibleStopper1
local debounce = false

ClickyDetector.MouseClick:Connect(function()
	
		if script.Parent.Color == Color3.fromRGB(255, 0, 0) and debounce == false then
			if debounce then return end
			print("succesfully registered click")
			InvisibleStopper1.Transparency = 1
			InvisibleStopper1.CanCollide = false
			InvisibleStopper1.CastShadow = false
			script.Parent.Color = Color3.fromRGB(0, 255, 0)
			task.wait(2)
		
	elseif script.Parent.Color == Color3.fromRGB(0, 255, 0) and debounce == false then
			if debounce then return end
			print("succesfully registered click")
			InvisibleStopper1.Transparency = 0.75
			InvisibleStopper1.CanCollide = true
			InvisibleStopper1.CastShadow = true
			script.Parent.Color = Color3.fromRGB(255, 0, 0)
			task.wait(2)
		end
	
end)

I showed you what the if statements would look like in the first reply.
Here’s what it should look like:

ClickyDetector.MouseClick:Connect(function()
		if debounce then return end
		if script.Parent.Color == Color3.fromRGB(255, 0, 0) then
			debounce = true
			print("succesfully registered click")
			InvisibleStopper1.Transparency = 1
			InvisibleStopper1.CanCollide = false
			InvisibleStopper1.CastShadow = false
			script.Parent.Color = Color3.fromRGB(0, 255, 0)
			task.wait(2)
			debounce = false
		
	elseif script.Parent.Color == Color3.fromRGB(0, 255, 0) then
			debounce = true
			print("succesfully registered click")
			InvisibleStopper1.Transparency = 0.75
			InvisibleStopper1.CanCollide = true
			InvisibleStopper1.CastShadow = true
			script.Parent.Color = Color3.fromRGB(255, 0, 0)
			task.wait(2)
			debounce = false
		end
	
end)
2 Likes

Ah, sorry, my bad. It’s really late at night XD

1 Like

That’s understandable, I also usually start thinking slow at night, lol

2 Likes

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