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.

script.Parent.Touched:Connect(function(hit)
    local debounce = true
    if debounce then
        debounce = false
    local model = game.Workspace.MALP
    if hit == game.Workspace.MALP.Part1
    then
      model:MoveTo(Vector3.new(-335.75, 31.154, -985.621))
    end
    end
 debounce = true
    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.

local Debounce = false

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

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:

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

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:

local model = game.Workspace.MALP
if hit == game.Workspace.MALP.Part1 then

--To This VVV

local model = game.Workspace.MALP
if 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