Hi can someone answer me a simple question

So i just need someone to answer me a really simple question.

You know some scripts where you turn things on and off, for example: turning something green if you click a clickdetector then turning it red if you click again, i think that’s a good example.

For that i usually make two scripts, one when you click first time and another one for the second time, each doing their part and switching with script.Disabled, but how can i do that in the same script?

Note: It doesn’t need to be using the part example you can use whatever you want as an example or just use – Script here

2 Likes

Not gonna lie i should already know that

uh… ClickDetector.Clicked:Connect(function(part) part.Color = Color3.new.fromRGB(green value) else part.Color = Color3.new.fromRGB(red value) end)

2 Likes

i was not talking specifically for changing colors that was just an example the real question was with the clicking but i think that solves the question i really should know that already, thanks

2 Likes

and no, you dont need to add another script just use the else method

2 Likes

Can someone explain, why does this work? I don’t get it.

Where is the ‘If’? I don’t see what condition is being evaluated for the ‘else’

I would expect this to always set the part to ‘green value’

1 Like

local part = – the part in the workspace example: game.workspace.part

part.ClickDetector.MouseClick:Connect(function()
part.Color = “green”
else
part.Color = “red”
end)

fun fact: Idk If this is right but you should try it I guess

1 Like

The marked solution… does not work…? (was a different solution) I don’t think you can use else if the code checks nothing and ClickDetector uses MouseClick instead of Clicked.

But you can do-- (example with black and white)

part.ClickDetector.MouseClick:Connect(function()
	if part.Color == Color3.fromRGB(0, 0, 0) then
		part.Color = Color3.fromRGB(255, 255, 255)
	else
		part.Color = Color3.fromRGB(0, 0, 0)
	end
end)
1 Like

Your code has a lot of problems… First your code doesnt have if statements correctly set up as @Sir_Highness said.

That is the wrong parameter, the correct paramenter is the player who clicked not the part which is being clicked.

Also please format it, it is almost not readable.

1 Like

What condition is the function evaluating to determine which statement to execute? How is anything changing from click to click, to click? I don’t see how this would toggle. I don’t doubt that it works, just trying to understand why it would work.

1 Like

well he wanted an example on how to use the detector so I only pinpoint this

script.ClickDetector.Clicked:Connect(function(part)
part -- color changed  -- yeah u know what this 
else -- click again
part --colorchanged again
end)

I dont care if it doesnt work he probably wants to know the clicked event and then function fires

2 Likes

I thought you were showing us some cool trick of using ‘else’ without ‘if’

1 Like

I just want to know how i can make it so if the player presses twice it does something different

** Then Yes** you only need the else because if clicked again the part wont become green forever, it will become red by using else please understand :slight_smile:

you could set up a counter variable outside the function and just increment and do stuff based on that value, or if its a simple toggle use a bool and flip it with ‘not’

1 Like

ok, but what is evaluated to false to trigger the ‘else’?

I have a script for this to make doors unlocking and locking. I am just giving you the part where you change the colour

local green = BrickColor.new("LimeGreen")
local Red = BrickColor.new("ReallyRed")

script.Parent.MouseClick:Connect(function() 
    if script.Parent.Parent.BrickColor == green then -- Check if part is green
        script.Parent.Parent.BrickColor = red -- If green make it red
    else --If part not green then set it to green
        script.Parent.Parent.BrickColor = green
    end
--This is assuming you have the part and the click detector 
end) 

Edit: Added more notes

2 Likes

Click first time: Green
Click again: Dont be green again instead else do Red

heck
the guy above me explains better so his code
if the part was clicked, then part should be green, If already green because the == green then turn red Otherwise/ else do color Green again

This entire thread is a fantastic example of blind teaching, or teaching before knowing.

I don’t mean to blow the whistle, but this should be obvious. Don’t teach Lua, if you don’t know Lua. You may want Internet karma, but #programming-help surely isn’t the place for that.


As for solving the original inquisition, the answer is cunningly simple.

local is_green = true

Part.ClickDetector.MouseClick:Connect(function()
    is_green = not is_green
    --[[ quick and dirty version of:
    if is_green then
        is_green = false
    else
        is_green = true
    end
    ]]

    if is_green then
        Part.Color = Color3.new(0, 1, 0) -- green
    else
        Part.Color = Color3.new(1, 0, 0) -- the epitome of not green
    end
end)

You can further compress this to a two-liner, using a Lua ternary

local is_green = true

Part.ClickDetector.MouseClick:Connect(function()
    is_green = not is_green
    Part.Color = is_green and Color3.new(0, 1, 0) or Color3.new(1, 0, 0)
end)
2 Likes
Code
local clicked = false

script.Parent.Clicked:Connect(function()
    if clicked == false then
        clicked = true
        script.Parent.Parent.BrickColor = BrickColor.new() ––red

    else
    clicked = false
    script.Parent.Parent.BrickColor = BrickColor.new() –—green
end