Player Rotation Glitch While Moving and Holding 'E'"

Hi i have this script that when you hold e it will make the player rotate to the mouse but theres a glitch where wehn player is walking while holding e theyre glitching while walking anyone know how to fix this

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

local keybind = Enum.KeyCode.E
local isHolding = false

local function rotateToMouse()
	while isHolding do
		if character and character:FindFirstChild("HumanoidRootPart") then
			local rootPart = character.HumanoidRootPart
			local targetPosition = Vector3.new(mouse.Hit.Position.X, rootPart.Position.Y, mouse.Hit.Position.Z)
			local direction = (targetPosition - rootPart.Position).Unit
			local targetCFrame = CFrame.new(rootPart.Position, rootPart.Position + direction)

			-- Tween the rotation
			local tween = TweenService:Create(rootPart, TweenInfo.new(0.1, Enum.EasingStyle.Linear), { CFrame = targetCFrame })
			tween:Play()
			tween.Completed:Wait()
		end
		task.wait(0.05) 
	end
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == keybind then
		isHolding = true
		rotateToMouse()
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == keybind then
		isHolding = false
	end
end)


local function updateCharacterMovement()
	while true do
		if character and character:FindFirstChild("Humanoid") then
			local humanoid = character.Humanoid
			if isHolding then

				humanoid.AutoRotate = false
			else
				humanoid.AutoRotate = true
			end
		end
		task.wait(0.1)  
	end
end

-- Start character movement update
coroutine.wrap(updateCharacterMovement)()

Very simple fix after some testing. All you need to do is change your tweeninfo time property to something lower like 0.01 so the movement flows smoother.

2 Likes

Thanks for the responce ill let you know later if its fixed.

I assume you’re holding W and E while walking in the video?
I think it’s because you’re tweening the rotation of the player, but you’re not telling it that the WASD commands are relative to the player so the player just keeps walking away from the camera like it should when W is pressed.