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

How would I find the speed of a moving part?

Asked by 3 years ago
Edited 3 years ago

This is a script I made that if the part touches something and its X velocity is higher than 20 it prints "touched" but I realized that the numbers would go into the negatives. How would I find the speed of the part without it going into the negatives?

local vel = script.Parent.Parent.Parent.Parent.Part.Velocity.X
local minvel = 20
script.Parent.Touched:Connect(function()
    if not debounce then
        if vel > minvel then
            debounce = true
            print("touched")
            wait(0.5)
            debounce = false

        end
    end
end)

1 answer

Log in to vote
0
Answered by 3 years ago

In order to make the velocity positive, you can use the math.abs() in the math library.

local vel = script.Parent.Parent.Parent.Parent.Part.Velocity.X
local minvel = 20
script.Parent.Touched:Connect(function()
    if not debounce then
        if math.abs(vel) > minvel then --It will become an absolute value.
            debounce = true
            print("touched")
            wait(0.5)
            debounce = false

        end
    end
end)
0
This is wrong as the vel variable wont change as you indexed it from the get to. Feedekaiser 25 — 3y
Ad

Answer this question