Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I put a cooldown on this elevator script?

I tried making an elevator script (only the floor moves). It has two buttons, up and down. It works, but the problem is the cooldown. When you click up, you can still click down and it forces the elevatorfloor to go down even if it's still going up. Same thing vice versa. The problem is possibly debounces and cooldowns, but i'm like,, absolutely hoRRIBLE with that. A little help?

local TweenService = game:GetService("TweenService")
local up = script.Parent.eup
local down = script.Parent.edown
local elevatorfloor = game.Workspace.Elevatorfloor
local tweeninfo = TweenInfo.new(4,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0)
local debounce = false
	
local elevatorfloordown = {CFrame = CFrame.new(-209, 0.7, -92.77)}
local elevatorfloorup = {CFrame = CFrame.new(-209, 22.91, -92.77)}
local tween1up = TweenService:Create(elevatorfloor,tweeninfo,elevatorfloorup)
local tween2down = TweenService:Create(elevatorfloor,tweeninfo,elevatorfloordown)

up.ClickDetector.MouseClick:Connect(function()
	if debounce == false then
		debounce = true
		tween1up:Play()
		tween1up:Enum.PlaybackState.Completed
	debounce = false
end)

down.ClickDetector.MouseClick:Connect(function()
	if debounce == false then
		debounce = true
		tween2down:Play()
		tween2down:Enum.PlaybackState.Completed
	debounce = false 
end)

1 comment

0

I edited my answer

1 answer

✓ Accepted answer

You should make a debounce variable and set it to false. When a player clicks one of the ClickDetectors, set it to true. Listen for the Completed event in Tween, and then in that function set debounce back to false so you can click one of the buttons again.

EDIT: You were close in implementing this. You have to :Connect the Completed event just as if you were doing it with a ClickDetector. Here is the best way of doing it, please try to understand how I did this!

tween1up.Completed:Connect(function()
    debounce = false
end)

tween2down.Completed:Connect(function()
    debounce = false
end)

up.ClickDetector.MouseClick:Connect(function()
    if debounce == false then
        tween1up:Play()
        debounce = true
    end
end)

down.ClickDetector.MouseClick:Connect(function()
    if debounce == false then
        tween2down:Play()
        debounce = true
    end
end)

2 comments

0

i edited the script,, is this right??

0

yas it is

This discussion is preserved as it was. New answers, comments, and votes are closed.