Apparently "Velocity" doesn't trigger the "Changed" event on a part. So, is there anyway to repeatedly get the velocity of a part without using a loop?
I ask this simply because i find events more efficient and practical than loops (in specific situations), and if there's a way to use one for this, i'd prefer it over using a loop.
Here's my arbitrary script that tracks a part's speed:
local Part = script.Parent local MaxVel = 200 while wait() do local speed = Part.Velocity.Magnitude if speed > MaxVel then print('Speed limit exceeded: '..MaxVel..' with '..math.floor(speed)) end end
If you know of any other way, i'd appreciate it. Thanks.
Physics and user input do not trigger .Changed
events. It would be problematic for physics to because of the continuous nature of physics and because of the way it's distributed over the network.
Because velocity changes constantly, it would be sort of silly to provide an event for it (because it would more just be an "often as possible event").
Of course, there is an often-as-possible event. game:GetService("RunService").RenderStepped
for a LocalScript (warning -- may not actually be as far as possible -- frame rate can be very low) or game:GetService("RunService").Heartbeat
.
Your code might look like this:
-- EDIT: Fixed bad typo game:GetService("RunService").RenderStepped:connect(function() local velocity = script.Parent.Velocity local speed = velocity.magnitude if speed > MAX_SPEED then print("Speed Limit ", MAX_SPEED, "exceeded at", math.floor(speed)) end end)
This is experimental and not advice -- this is just something I've been working on and may be interesting to you.
This is a use case for "functional reactive programming". The idea is that you describe how to react to things rather than react to the things themselves.
I am still actively improving this, but the most up-to-date code can be found here
To set a label to a message with a color for speeding, your code might look like this:
local S = require(script.Parent.Signals) -- the module local MAX_VEL = 100 local speed = S.FromProperty(script.Parent, "Velocity"):index("magnitude") local speeding = speed:map(function(speed) return speed > MAX_VEL end) -- RMK: will provide shorter syntax to this, -- likely `speeding:ifelse(Color3.new(1, 0, 0), Color3.new(0, 0, 0))` speeding:map(function(speeding) if speeding then return Color3.new(1, 0, 0) else return Color3.new(0, 0, 0) end end):set(label, "TextColor3"); ("You are going " .. speeding):set(label, "Text")