For example, if I want to damage all parts that hit a surface faster than <x> speed.
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.