AssemblyLinearVelocity not being read correctly by my script

My goal: Being able to read the current speed of my vehicle, in order to limit the driver to a max speed, if it has been reached.

My issue: My script considers that the speed (assemblylinearvelocity.Magnitude) is always 0 and is not changing, even though my car is clearly in motion.

local moverSeat = script.Parent.moverSeat

local vectorForce = script.Parent.Base.VectorForce
local force = 300
local forceVector = script.Parent.Base.CFrame.RightVector

local MAXSPEED = 20


moverSeat:GetPropertyChangedSignal("Throttle"):Connect(function()
	if moverSeat.Throttle == 1 then
		vectorForce.Force = forceVector * force
	elseif moverSeat.Throttle == -1 then
		vectorForce.Force = -forceVector * force
	elseif moverSeat.Throttle == 0 then
		vectorForce.Force = Vector3.new(0,0,0)
	end
end)

script.Parent.Base:GetPropertyChangedSignal("AssemblyLinearVelocity"):Connect(function()
	local currentSpeed = script.Parent.Base.AssemblyLinearVelocity.Magnitude
	print(currentSpeed) --this doesn't even output the speed
    if currentSpeed >= MAXSPEED then
	     vectorForce.Force = Vector3.new(0,0,0)
	end
end)
while true do
	print(speed)
	wait(1)
end

This always outputs “0”

Strangely enough, testing out the vehicle ingame, i wrote
print(game.Workspace.car.Base.AssemblyLinearVelocity.Magnitude)
into the command bar. I always got the speed value from this. But not from my script.

Because I receieved the value from the manual command but not from the script, I don’t believe that the root of the problem lies in how I built the car model.
It also makes me wonder how after a lot of searching I haven’t encountered someone else have this problem.

After further messing around, I seem to have found the (rather unexpected) solution.
Instead of having your script like this:

local speed  = script.Parent.AssemblyLinearVelocity.Magnitude

while true do
	print(speed)
	wait(1)
end

You should do something like:

local speed  

while true do
	speed = script.Parent.AssemblyLinearVelocity.Magnitude
	print(speed)
	wait(1)
end

this.

Basically, your variable of the speed needs to get occasionally updated.

1 Like

My follow up question, is why :GetPropertyChangedSignal(“AssemblyLinearVelocity”) doesn’t fire when it should. Is this a bug or intentional?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.