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:
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)
A module I'm making
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)
local MAX_VEL = 100
local speed = S.FromProperty(script.Parent, "Velocity"):index("magnitude")
local speeding = speed:map(function(speed) return speed > MAX_VEL end)
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")
3 comments
Velocity does trigger it. Any property will as long as it's actually changed.
Make sure you're using a regular script, and not a local script.
Actually it doesn't.