Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by
Oficcer_F 207 Moderation Voter
6 years ago

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!

Ad

Answer this question