Player orientation not saving when moving

I have been trying to make a tank movement system for my game but I have been stuck on trying to rotate the player when A or D is pressed but the tank modal is not rotating with the HumanoidRootPart and the rotation is reset after the player moves

Movement Script not working Recording

(A simplified version of my code)
(Also I am using a simple state machine for the held variables)

local MoveSpeed = 0.25
local WIsHeld = false
local AIsHeld = false

local HumanRoot = script.Parent:WaitForChild("HumanoidRootPart")

while true do
	if WIsHeld then
		script.Parent:PivotTo(CFrame.new(script.Parent:GetPivot().Position + HumanRoot.CFrame.LookVector * MoveSpeed))
	end
	if AIsHeld then
		
		local NewOrientation = HumanRoot.Orientation + Vector3.new(0,1,0)
		script.Parent.PrimaryPart.Orientation = NewOrientation
	end

	task.wait(0.01)

end

3 Likes

You should try to rotate it by manipulating CFrame property instead of Orientation property I think.

2 Likes

Maybe the PrimaryPart is not the HumanoidRootPart

Do this:

local NewOrientation = HumanRoot.Orientation + Vector3.new(0,1,0)
HumanRoot.Orientation = NewOrientation

The primary part is the HumanoidRootPart of the modal

1 Like

I tried using the CFrame.Orientation of the part but it gave me a error saying “Orientation cannot be assigned to”

1 Like

Try this:

local MoveSpeed = 0.25
local WIsHeld = false
local AIsHeld = false

local HumanRoot = script.Parent:WaitForChild("HumanoidRootPart")

while true do
	if WIsHeld then
		script.Parent:PivotTo(CFrame.new(script.Parent:GetPivot().Position + HumanRoot.CFrame.LookVector * MoveSpeed))
	end
	if AIsHeld then

		local NewOrientation = HumanRoot.Orientation + Vector3.new(0,1,0)
		script.Parent.PrimaryPart.CFrame *= CFrame.Angles(math.rad(NewOrientation.X), math.rad(NewOrientation.Y), math.rad(NewOrientation.Z))
		--script.Parent.PrimaryPart.Orientation = NewOrientation
	end

	task.wait(0.01)

end

The HumanoidRootPart moves with the modal but it still resets the rotation when the W key is pressed

Because you are Pivoting the model to a CFrame with only the positional aspect and ignoring the rotational aspect of the model. Do this instead:

script.Parent:PivotTo(script.Parent:GetPivot() * CFrame.new(0, 0, -MoveSpeed))
2 Likes

This works but the modal itself is not turning

Try this?

local MoveSpeed = 0.25
local WIsHeld = false
local AIsHeld = false

local HumanRoot = script.Parent:WaitForChild("HumanoidRootPart")

while true do
    local cframe = CFrame.new(0, 0, 0)
	if WIsHeld then
		cframe *= CFrame.new(0, 0, -MoveSpeed)
	end

	if AIsHeld then
		cframe *= CFrame.Angles(0, math.rad(1), 0)
	end

    script.Parent:PivotTo(script.Parent:GetPivot() * cframe)

	task.wait(0.01)

end

I solved the no rotating problem by using this in the

If AIsHeld then
   script.Parent:PivotTo(CFrame.new(script.Parent:GetPivot().Position) * CFrame.Angles(math.rad(NewOrientation.X),math.rad(NewOrientation.Y),math.rad(NewOrientation.Z)
end

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