sometimes, long codes are ugly. just look at this normal door code:
--normal
script.Parent.ClickDetector.MouseClick:Connect(function()
if script.Parent.Transparency == 0 then
script.Parent.Transparency = 1
script.Parent.CanCollide = false
else
script.Parent.Transparency = 0
script.Parent.CanCollide = true
end
end)
it works, but you probably dont wanna write that, you want something shorter
so, lets just clear that code.
script.Parent.ClickDetector.MouseClick:Connect(function()
end)
now, how do we make a toggle a property without ifs? lets just think of an method.
okay, so to toggle the cancollide we will use the following method:
print(true) -- true
print(not true) -- false
print(false) -- false
print(not false) -- true
get it? so this snippet would toggle the cancollide property:
script.Parent.CanCollide = not script.Parent.CanCollide
lets add that to our code
script.Parent.ClickDetector.MouseClick:Connect(function()
script.Parent.CanCollide = not script.Parent.CanCollide
end)
now, what about transparency? not script.Parent.Transparency would not work, so this one is more complex but im pretty sure you will get the method.
print(1 and 0 or 1) -- 0
that was creepy, but it prints 0, so now what about it being 0?
print(0 and 0 or 1)
its still, 0, not toggleable, so we will change the code a bit
print(1 == 1 and 0 or 1) -- 0
print(0 == 1 and 0 or 1) -- 1
it worked! now this is what it should be:
script.Parent.Transparency = (script.Parent.Transparency == 1 and 0 or 1)
a bit difficult right? but its shorter, so now your new cleaner code should look like this:
script.Parent.ClickDetector.MouseClick:Connect(function()
script.Parent.CanCollide = not script.Parent.CanCollide
script.Parent.Transparency = (script.Parent.Transparency == 1 and 0 or 1)
end)
and yes, it does work.
now what if i want to toggle colors?
well dont return to if for that, you could do the same method of the transparency thing we just did.
script.Parent.BrickColor = (script.Parent.BrickColor == BrickColor.Red() and BrickColor.Green() or BrickColor.Red())
longer but still shorter and better than if.
now if youre making an actual opening door, you could do something similar like this
script.Parent.ClickDetector.MouseClick:Connect(function()
script.Parent.ClickDetector.MaxActivationDistance = 0 -- because they can break it by spam-clicking
game:GetService("TweenService"):Create(script.Parent,TweenInfo.new(2),{Position=(script.Parent.Position == Vector3.new(-2.7, 6.05, -34.2) and Vector3.new(-2.7, 6.05, -11.4) or Vector3.new(-2.7, 6.05, -34.2))}):Play()
task.wait(5)
script.Parent.ClickDetector.MaxActivationDistance = 32
end)
yeah reading it just puts salt in your eyes but i hope you get it
now you know how to simplify toggling codes, this could save time.
ifs could be used for something else, but if youre toggling something then this method is better than if
i know this was short
if you’re confused or want to send feedbacks, go on.