Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make player face the mouse?

I have been trying to figure out how to make the player face the mouse, i am currently new to BodyGyro and other things.

local player = game.Players.LocalPlayer
local character = player.Character
local torso = character.LowerTorso
local BodyGyro = Instance.new("BodyGyro", torso)
local bodyGyro = torso.BodyGyro
local mouse = player:GetMouse()


mouse.Move:connect(function()
    bodyGyro.CFrame = CFrame.new(torso.Position, mouse.Hit.p)
end)

4 answers

local player = game.Players.LocalPlayer
local gyro = nil
local mouse = player:GetMouse()
mouse.TargetFilter = game.Workspace.Level
local runService = game:GetService('RunService')
local character = player.Character or player.CharacterAdded:wait()-- use this
 
local function onCharacterAdded(character)
	local head = character:WaitForChild('Head') -- Use head for R15
	gyro = Instance.new('BodyGyro')
	gyro.P = 50000
	gyro.MaxTorque = Vector3.new(0, 10000, 0)
	gyro.Parent = head
 
	local humanoid = character:WaitForChild('Humanoid')
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
end
 
local function isnan(x) return x ~= x end
 
local function onRenderStep()
	if gyro then
		local mouseHit = mouse.Hit.p
		if not (isnan(mouseHit.X) or isnan(mouseHit.Y) or isnan(mouseHit.Z)) then
			gyro.CFrame = CFrame.new(character.Head.Position, mouseHit)
		end
	end
end
 
while not player.Character do wait() end
onCharacterAdded(player.Character)
player.CharacterAdded:connect(onCharacterAdded)
 
runService:BindToRenderStep('TrackMouse', Enum.RenderPriority.Input.Value, onRenderStep)

That's my script for this. Hope this helps :)

1 comment

0

Doesnt work for me, sorry.

Put the BodyGyro in HumanoidRootPart, also this should be converted into FilteringEnabled

RunService = game:GetService('RunService') Players = game:GetService('Players') Player = Players.LocalPlayer Mouse = Player:GetMouse() Char = Player.Character if not Char then Player.CharacterAdded:Wait() Char = Player.Character end

RootPart = Char:WaitForChild('HumanoidRootPart')

RunService.Stepped:connect(function()

local MousePos = Mouse.Hit.p

local lookVector = Vector3.new(MousePos.X,RootPart.CFrame.Y,MousePos.Z)

RootPart.CFrame = CFrame.new(RootPart.CFrame.p,lookVector)

end)

1 comment

0

there, hope it helps. also put this in StarterPlayer.StarterPlayerScript and this mst be a local script

I think your problem is in local mouse = player:getMouse() as the computer doesn't know what player. If this script is in a starterpack or startergui then you could do script.Parent.Parent:GetMouse() which is what worked for my script

2 comments

0

No, he's doing it right. Player is a variable he has already.

0

Maybe it's because im using r15?

This discussion is preserved as it was. New answers, comments, and votes are closed.