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