Mouse Facing script not working

I wrote a script that makes the players torso face the mouse whenever active.Value == true (boolean value), but for some reason, it doesn’t work. I wrote it in different ways, and the only way for it to work is if it’s not checking for whether active is true, why?

local players = game:GetService("Players")
local plr = players.LocalPlayer
local Mouse = plr:GetMouse()
local Character = plr.Character or plr.CharacterAdded:Wait() 
local HumRP = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local active = script.Parent.Active 

coroutine.resume(coroutine.create(function()
	while true do
		wait()
		if active.Value == true then
			local aim = CFrame.new(HumRP.Position, Vector3.new(Mouse.Hit.p.X, HumRP.Position.Y, Mouse.Hit.p.Z))
			local direction = aim.LookVector

			local headingA = math.atan2(direction.X, direction.Z)
			headingA = math.deg(headingA)

			HumRP.CFrame = CFrame.new(HumRP.Position) * CFrame.Angles(0, math.rad(headingA), 0)
		end
	end
end))

Disable AutoRotate, or use BodyGyro Instead.

Here, this should work though

local players = game:GetService("Players")
local plr = players.LocalPlayer
local Mouse = plr:GetMouse()
local Character = plr.Character or plr.CharacterAdded:Wait() 
local HumRP = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local active = script.Parent.Active 

coroutine.resume(coroutine.create(function()
	while true do
		wait()
		if active.Value == true then
			local LookAtCFrame = CFrame.new(HumRP.Position, Mouse.Hit.Position)
			local _,Yaw,_ = LookAtCFrame:ToOrientation()
			
			HumRP.CFrame = CFrame.new(HumRP.Position) * CFrame.Angles(0,Yaw,0)
		end
	end
end))

thank you! also… i made a mistake in the original script that changed actives value… so that contributed to the problem, haha. again, thank you!!!

1 Like

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