Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Getting velocity when changed without a loop?

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.

3 comments

0

Velocity does trigger it. Any property will as long as it's actually changed.

0

Make sure you're using a regular script, and not a local script.

0

Actually it doesn't.

1 answer

✓ Accepted answer

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)

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) -- 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")

2 comments

0

Thank you very much, i appreciate the explanation and reference.

0

Oops, I had a really bad typo in my code. Fixed.

This discussion is preserved as it was. New answers, comments, and votes are closed.