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

Why won't my player look at mouse.hit?

Local script in starterplayerscripts.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character

local function onCharacterAdded(character)
	while true do
    if not char or not char.Parent then
        char = player.CharacterAdded:Wait()
    end
    local hrp = char:WaitForChild("HumanoidRootPart")
    char.PrimaryPart = hrp
    if mouse.Hit then
        local lookAt = Vector3.new(mouse.Hit.p.X, hrp.Position.Y, mouse.Hit.p.Z)
        char:SetPrimaryPartCFrame(CFrame.new(hrp.Position, lookAt))
    end
end
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

1 answer

✓ Accepted answer

Put this in StarterCharacterScripts, it will load with your character. SetPrimaryPartCFrame seems to cause problems, so I'm setting it a different way.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character

local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")

hum.AutoRotate = false


while hum and hum.Health > 0 do 
	if mouse.Hit then 
		local lookAt = Vector3.new(mouse.Hit.p.X, hrp.Position.Y, mouse.Hit.p.Z)
		local cf = CFrame.new(hrp.CFrame.p, lookAt)
		hrp.CFrame = cf
	end
	wait()
end

1 comment

0

I get: PlayerScripts.LocalScript:6: attempt to index local 'char' (a nil value)

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