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