#What statement should I use

1 messages · Page 1 of 1 (latest)

real saddle
#

Hello, I'm new to scripting.
I've successfully coded a button that turns red when u click it and green when u click it again.
However, I'm trying to make it so after 7 seconds since the last time the button had been clicked red, it turns green again.
What statement should I use?

soft surge
#

task.wait(7) then change the color back to green

indigo rootBOT
#

studio** You are now Level 2! **studio

real saddle
#

I guess I wanna make a timer, everytime the button turns red, it will set a timer until 7 secs before it turns green again.

#

However if it becomes green before 7 sec, the timer resets.

soft surge
#

One way to solve this is to make a variable that is increased as soon as the button is pressed, and create a new variable that you will compare it to after the 7 seconds to make sure it has not changed.

#

so if the variable has changed (meaning it was clicked) you will return instead of making it green

#

something like this

local timesClicked=0
script.Parent.ClickDetector.MouseClick:Connect(function()
-- change the color to either red or green as you were doing,
timesClicked+=1
if red then -- replace with proper if statement
local n=timesClicked
task.wait(7)
if timesClicked==n then
-- change the color back to green
end
end
end)

queen cosmos
#

i think theres a better way

#
local lasttimeclicked = nil
...clicked:con(function)
   lasttimeclicked = tick()
 task.wait(7)
if lasttimeclicked - tick > 7 then
print("turning red")
end
end
#

u could use task.defer

#

task.delay

#

or whatever

#

it all works anyway

real saddle
#

thank u guys!!