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

Am I properly using Debounce here?

Asked by 5 years ago

Because when I bring the model to the position, it still lags and bounces around.

01script.Parent.Touched:Connect(function(hit)
02    local debounce = true
03    if debounce then
04        debounce = false
05    local model = game.Workspace.MALP
06    if hit == game.Workspace.MALP.Part1
07    then
08      model:MoveTo(Vector3.new(-335.75, 31.154, -985.621))
09    end
10    end
11 debounce = true
12    end)

For context, I am moving a car to this position. It does not lag or bounce around before I bring it to the destination.

0
If you will be using Debounce in many of your scripts, consider making a module/function that does this for you (i.e. https://developer.roblox.com/articles/Debounce at "Advanced Notation") saSlol2436 716 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Correct me if I'm wrong, but I think you gotta define the variable "Debounce" outside of the function.

01local Debounce = false
02 
03script.Parent.Touched:Connect(function(hit)
04    if Debounce then return end --Checking if the variable is true. If not, continue.
05        Debounce = true --Now debounce is true and firing this function will stop the code at the line above
06        local model = game.Workspace.MALP
07        if hit == model.Part1 then
08            model:MoveTo(Vector3.new(-335.75, 31.154, -985.621))
09        end
10        Debounce = false--Now the code in the function is ready to be fired again.
11    end
12end)

Made the script look a little more cleaner, but I hope you understand how debounce is used. Another thing to have in your head while programming is the use of variables.

You made this:

1local model = game.Workspace.MALP
2if hit == game.Workspace.MALP.Part1 then
3    model:MoveTo(Vector3.new(-335.75, 31.154, -985.621))
4end

To make the script a little cleaner you can use the model that you just defined with a variable to make shorter lines like this:

1local model = game.Workspace.MALP
2if hit == game.Workspace.MALP.Part1 then
3 
4--To This VVV
5 
6local model = game.Workspace.MALP
7if hit == model.Part1 then --This line got shorter

I hope this helped you out. Just comment if there's anything you're wondering about or if it's something i've done wrong.

Waterfox

Ad

Answer this question