Would this work?

Would this work? This is just an example but imagine i’m making a close and open script, if i press it it will close and if i press it again it opens, i usually make two scripts for this so i’m trying this now, so would this script work:

local idkaname = false
script.Parent.ClickDetector.MouseClick:Connect(function()
         if idkaname == false then
         -- Code  if the player presses once
         idkaname == true
         end
elseif idkaname == true then
                 -- code if the player presses twice
                 idkaname = false
         end
end)

no try this script

local idkaname = false
script.Parent.ClickDetector.MouseClick:Connect(function()
if idkaname == false then
– Code if the player presses once
idkaname = true
end
elseif idkaname == true then
– code if the player presses twice
idkaname = false
end
end)

First off, format your code.

Second, just flip the state of the bool, then make a check.

local state = false

clickDetector.MouseClick:Connect(function() 
    state = not state

    if state then
        — maybe a door was opened or something
    else
        — close that door
    end

end)
3 Likes

There’s never a need to use elseif on a boolean, else does the job better, and I believe it is slightly more efficient.

3 Likes