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

Script goes crazy, how to put a cooldown on it?

Hello, I've got this kind of simple script here, it works like this:

You touch a part, and then a model moves.

The problem is, if you keep touching the part, which isn't supposed to happen, the script goes nuts and moves way too fast.

How do I fix this?

Script:

workspace.BlockingGate.PrimaryPart = workspace.BlockingGate.Center

script.Parent.Touched:connect(function()
	for i = 1,100 do
		wait()
		
		workspace.BlockingGate:SetPrimaryPartCFrame( CFrame.new(workspace.BlockingGate.PrimaryPart.Position + Vector3.new(0,-0.5,0)) )
		
	end
	
	wait (2)
	


for i = 1,100 do
		wait()
		
		workspace.BlockingGate:SetPrimaryPartCFrame( CFrame.new(workspace.BlockingGate.PrimaryPart.Position + Vector3.new(0,-0.5,0 )) )
		
	end
	
	wait (2)
	
end)

Thanks for helping

1 answer

✓ Accepted answer

You need to add a debounce:


debounce = false
workspace.BlockingGate.PrimaryPart = workspace.BlockingGate.Center
script.Parent.Touched:connect(function()
if debounce == false then
debounce = true
    for i = 1,100 do
        wait()

        workspace.BlockingGate:SetPrimaryPartCFrame( CFrame.new(workspace.BlockingGate.PrimaryPart.Position + Vector3.new(0,-0.5,0)) )

    end

    wait (2)



for i = 1,100 do
        wait()

        workspace.BlockingGate:SetPrimaryPartCFrame( CFrame.new(workspace.BlockingGate.PrimaryPart.Position + Vector3.new(0,-0.5,0 )) )

    end

    wait (2)
end
wait(5) --How long you cannot touch it 
debounce = false 
end)

This should work!

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