Hey there I’m just starting out with scripting and I wish for some help on this script that I’m trying to understand.
So what it is is a script where you touch something and then an object appears then goes away and then a cooldown then it works again.
All I need help is with making it when u touch the part (which is transparent and can collide is off) that it makes the other part pop out then I think I can fade it away then a cooldown before the script that triggers the other part to pop out to have a cooldown before working again. Please help me if u can
For a cooldown you would normally use a ‘debouncer’, usually a variable in an outside scope to track whether the event was fired recently or not.
local button = script.Parent -- assuming this script is located inside the trigger brick
local otherPart = script.Parent.Parent.OtherPart -- whatever the part is
local ready = true
local cooldownTime = 5
button.Touched:Connect(function()
if (ready) then
ready = false
otherPart.Transparency = 0
wait(cooldownTime)
-- fade out
for i = 0, 1, 0.1 do
otherPart.Transparency = i
wait()
end
-- reset the button
ready = true
end
local cooldown = 5
local db = false
script.Parent.Touched:Connect(function(hit)
if not db then
db = true
-- code for popping out the part
wait(cooldown)
db = false
end
end)
local cooldown = 0
local interval = 1 -- in seconds
if(os.time() >= cooldown)then
--start the cooldown
cooldown = os.time() + interval
-- do something
end
if you want to include milliseconds then look into os.clock() instead of os.time()
its not generally recommended to use wait() for timers/cooldowns, only to make things sleep/yield temporarily. (without worry about time accuracy)
using the method above also means you dont have to create any loops nor do you have to yield anything, so it can come out more optimized in such a “as needed” situation as this.
thats okay, once you get into more complex/longterm timers/cooldowns you’ll see why wait() isn’t helpful but i suppose its okay for temporary debounces. (especially if your not worried about the accuracy, wait() can wait alot longer than you want sometimes)