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

How do I check how fast a part is going?

Asked by
P100D 590 Moderation Voter
9 years ago

For example, if I want to damage all parts that hit a surface faster than <x> speed.

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Parts have a .Velocity property, which is a vector stating how many studs/second ROBLOX is trying to make the object move.

To get the speed of the part, you can get the .magnitude of the Velocity.

A problem with this approach is that on contact with a surface, ROBLOX may rebound / reduce the speed before your script gets a chance to grab its original velocity.

You can fix that by tracking objects before hand, e.g.

local speed = 0
while true do
    wait()
    local nowSpeed = part.Velocity.magnitude
    -- The speed right now
    speed = speed * 0.5 + nowSpeed * 0.5
    -- We use "speed" instead which is just a bit of a "smoother" speed 
    -- over time.
end

function touched()
    -- Going "speed" studs per second
end

Or, if we're going to be doing this ourselves, we could compute velocity by keeping track of the position it was at and is at now.

A problem with this is that, as I said before, Velocity is the speed ROBLOX is trying to make the object move at. While it might move very slowly for a short bit, once the server has less work to do it it might make it start moving faster.

The Velocity reflects the "actual" velocity; e.g., parts that have a high velocity will hit other objects "harder" (move them more) even if the server shows it moving slower.

0
Oh! I never knew about Velocity. Sorry for a bad answer yumtaste 476 — 9y
Ad

Answer this question