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