Is it possible to detect if a part is at a certain speed?

I’m trying to make a crash script for my Aircraft using Velocity detection but I don’t know how to use it properly resulting Error.

local VehicleSeat = script.Parent.Parent:WaitForChild("VehicleSeat")
local DestroyParticle = script.Parent.Parent:WaitForChild("DestroyParticle")
local Velocity = VehicleSeat.Velocity.magnitude;

if Velocity.Speed == 10 then
	DestroyParticle.Enabled = true
end

I’m still learning Velocity so I don’t really know if this is the right code.

3 Likes

You need to use AssemblyLinearVelocity like so:

local VehicleSeat = script.Parent.Parent:WaitForChild("VehicleSeat")
local DestroyParticle = script.Parent.Parent:WaitForChild("DestroyParticle")
local Velocity = VehicleSeat.AssemblyLinearVelocity.Magnitude;

if Velocity == 10 then
	DestroyParticle.Enabled = true
end
5 Likes

Dont point to the magnitude itself instead point to the part then do

if part.AssemblyLinearVelocity.Magnitude == 10 then
end

If you point to the Magnitude the script will reference it only the first time and then never update it.

3 Likes

Whether you use:

or use:

won’t make a difference unless you want to check the value within a loop

2 Likes

Yeah, but still wanted to point it out as it can lead to issues later. I learned it the hard way.

3 Likes

True, if @officer_wiki wants to eventually check the velocity within a loop they’ll need to do something like this:

local VehicleSeat = script.Parent.Parent:WaitForChild("VehicleSeat")
local DestroyParticle = script.Parent.Parent:WaitForChild("DestroyParticle")

local connection
connection = game:GetService"RunService".PostSimulation:Connect(function()
	if VehicleSeat.AssemblyLinearVelocity.Magnitude == 10 then
		connection:Disconnect()
		connection = nil

		DestroyParticle.Enabled = true
	end
end)

Edit: Also because of floating-point errors the magnitude might be a value like 10.00000000000008 or something like that instead of exactly 10, which would cause the loop to keep running instead of enabling the DestroyParticle. To solve this you’ll need to round the result like so:

local VehicleSeat = script.Parent.Parent:WaitForChild("VehicleSeat")
local DestroyParticle = script.Parent.Parent:WaitForChild("DestroyParticle")

local connection
connection = game:GetService"RunService".PostSimulation:Connect(function()
	if math.round(VehicleSeat.AssemblyLinearVelocity.Magnitude) == 10 then
		connection:Disconnect()
		connection = nil

		DestroyParticle.Enabled = true
	end
end)

Or if you don’t mind a very slightly imprecise value you could also do:

local VehicleSeat = script.Parent.Parent:WaitForChild("VehicleSeat")
local DestroyParticle = script.Parent.Parent:WaitForChild("DestroyParticle")

local connection
connection = game:GetService"RunService".PostSimulation:Connect(function()
	if VehicleSeat.AssemblyLinearVelocity.Magnitude >= 10 then
		connection:Disconnect()
		connection = nil

		DestroyParticle.Enabled = true
	end
end)
3 Likes

I just got back. I’ll test the scripts now

I’m sorry gentlemen but these didn’t work.

The script should have enabled the particle like this but didn’t work.

This should have worked, if it didn’t then there’s something else that’s blocking the particles from turning on

local VehicleSeat = script.Parent.Parent:WaitForChild("VehicleSeat")
local DestroyParticle = script.Parent.Parent:WaitForChild("DestroyParticle")

local connection
connection = game:GetService"RunService".PostSimulation:Connect(function()
	if VehicleSeat.AssemblyLinearVelocity.Magnitude >= 10 then
		connection:Disconnect()
		connection = nil

		DestroyParticle.Enabled = true
	end
end)

There’s nothing blocking it but it’s still broken. I made it print a text but it also doesn’t show up on the Logs

Are there any errors in the output log?

1 Like

Can you show the script? (only the broken part)

There are no Errors on the F9 Logs so I don’t know where the broken part is…

I used a car for a test and it also didn’t work.
image