Help me with a basic script

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 :slight_smile:

1 Like

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
4 Likes

Something like that:

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)
4 Likes
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.

3 Likes

Thank you so much this has helped a heck of a lot!

Thanks a lot!! :slight_smile: will this work for other code like changing colors?

Little bit confusing but I’ll try it!

yeah it IS a bit confusing at first, but its just comparing times.

so, first we checked if current time was bigger than the cooldown (which is true at FIRST because cooldown is zero).

after that if statement has ran, the cooldown will be os.time() + interval

so, now the if statement wont run again until os.time() reaches the time you set.

os.time() is constantly incrementing upwards too, so the time will be different everytime you call it.

1 Like

I think I’ll stick with the first one for this lol its still confusing. Oh! Is that just the cooldown?

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)

edit: and yes the above was just for the cooldown

1 Like

I’m not worried about the accuracy for this but thanks!