BodyAngularVelocity turning randomly

Hi, I’m working on some vehicles for my game, I’m using BodyAngularVelocity to turn them, however BodyAngularVelocity turns even without me pressing A/D in my vehicle

Here is my code:

local seat = script.Parent 
local speed = 0 
local bv = script.Parent.BodyVelocity
local bg = script.Parent.BodyAngularVelocity

while true do
	wait()
	if seat.Throttle == 1 then 
		if speed < seat.MaxSpeed then 
			speed = speed + 0.5 
		end
		
		bv.Velocity = script.Parent.CFrame.LookVector * speed 
	elseif seat.Throttle == 0 then 
		if speed > 0 then 
			speed = speed - 0.5 
		elseif speed < 0 then 
			speed = speed + 0.5 
		end
		
		bv.Velocity = script.Parent.CFrame.LookVector * speed 
		
	elseif seat.Throttle == -1 then 
		if speed > -seat.MaxSpeed then 
			speed = speed - 0.25 
		end
		
		bv.Velocity = script.Parent.CFrame.LookVector * speed 
	end
	
	seat.Changed:Connect(function()
		if seat.Steer == 1 then
			bg.AngularVelocity.Z = Vector3.new(0,0,1)
		elseif seat.Steer == -1 then
			bg.AngularVelocity.Z = Vector3.new(0,0,-1)
		elseif seat.Steer == 0 then
			bg.AngularVelocity.Z = 0
		end
	end)
end

And the settings on the BodyAngularVelocity:
image

Is there a known way to fix this, if so please help!

1 Like

Go to the View tab and enable the Output window: image

this will let you see errors and help you debug code. Here’s the kind of error you should be getting because of a bug in your code:

Z cannot be assigned to  -  Edit
Stack Begin  -  Studio
Script 'game.Workspace.BodyAngularVelocity.AngularVelocity.Z = 10', Line 1  -  Studio
Stack End  -  Studio

this is because you’re trying to assign to the Z component of AngularVelocity, which has type Vector3. Vector3s don’t support changing their XYZ components, which explains the error. Fix it by assigning an entirely new Vector like the other branches of the if statement.

If you’re not getting an error, it’s because your script never reaches the bg.AngularVelocity.Z = 0 branch, meaning your logic for checking steering is wrong somehow. If in doubt, set a breakpoint in that branch or just before the whole if statement to step through the code and understand how it executes.

Click in the gutter on the left of the script editor to set a breakpoint:
image

Step through your code with the debugger controls:
image

2 Likes