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