Rotating the player to face another player without rotating the X axis

Hello! I’m making a game where you have swords and other cool melees, and I decided to implement a lock on system similar to Dark Souls. I want the player to rotate to face the player when E is pressed, but the script I found ends up making the player rotate on the X axis when jumping, which I dont want.

https://gyazo.com/0350a84b939f3b6755c19d798897dccf

Heres the script I used:

local UIS = game:GetService("UserInputService")
local locked = false
local Players = game.Players
local player = Players.LocalPlayer
local Mouse = player:GetMouse()
local HRP = game.Workspace:WaitForChild(player.Name).HumanoidRootPart

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		if locked == false then
			local target = Mouse.Target
			if target ~= nil then
				if target.Parent:FindFirstChild("Humanoid") then
					locked = true
					local targetHRP = target.Parent.HumanoidRootPart
					targetHRP.BillboardGui.ImageLabel.Visible = true
					player.Character.Humanoid.AutoRotate = false
					while locked == true do
						HRP.CFrame = CFrame.new(HRP.Position, targetHRP.Position)
						wait()
					end
					targetHRP.BillboardGui.ImageLabel.Visible = false
					player.Character.Humanoid.AutoRotate = true
				elseif target.Parent.Parent:FindFirstChild("Humanoid") then
					local targetHRP = target.Parent.Parent.HumanoidRootPart
					player.Character.Humanoid.AutoRotate = false
					while locked == true do
						HRP.CFrame = CFrame.new(HRP.Position, targetHRP.Position)
						wait()
					end
					targetHRP.BillboardGui.ImageLabel.Visible = false
					player.Character.Humanoid.AutoRotate = true
				else 
					print ("aimed at NOT a player")
				end
			end
		elseif locked == true then
			locked = false
			player.Character.Humanoid.AutoRotate = true
		end
	end
end)

I’m not very familiar with CFrame, and all the things I found online weren’t very helpful either. Thanks in advance for any help

1 Like

Hey there! I believe this must fix your issue!

Also I would suggest you to use the ContextActionService instead of User Input Service.

local UIS = game:GetService("UserInputService")
local locked = false
local Players = game.Players
local player = Players.LocalPlayer
local Mouse = player:GetMouse()
local HRP = game.Workspace:WaitForChild(player.Name).HumanoidRootPart

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		if locked == false then
			local target = Mouse.Target
			if target ~= nil then
				if target.Parent:FindFirstChild("Humanoid") then
					locked = true
					local targetHRP = target.Parent.HumanoidRootPart
					targetHRP.BillboardGui.ImageLabel.Visible = true
					player.Character.Humanoid.AutoRotate = false
					while locked == true do
						HRP.CFrame = CFrame.new(HRP.Position,Vector3.new(targetHRP.Position.X, HRP.Position.Y, targetHRP.position.Z))
						wait()
					end
					targetHRP.BillboardGui.ImageLabel.Visible = false
					player.Character.Humanoid.AutoRotate = true
				elseif target.Parent.Parent:FindFirstChild("Humanoid") then
					local targetHRP = target.Parent.Parent.HumanoidRootPart
					player.Character.Humanoid.AutoRotate = false
					while locked == true do
						HRP.CFrame = CFrame.new(HRP.Position, targetHRP.Position)
						wait()
					end
					targetHRP.BillboardGui.ImageLabel.Visible = false
					player.Character.Humanoid.AutoRotate = true
				else 
					print ("aimed at NOT a player")
				end
			end
		elseif locked == true then
			locked = false
			player.Character.Humanoid.AutoRotate = true
		end
	end
end)
1 Like