So i making a character following the mouse and i got this error
Script:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.Camera
local rotationSpeed = 5
local maxLookUpAngle = math.rad(80)
local function RotateCharacter()
local mousePosition = player:GetMouse().X
local characterPosition = character:WaitForChild("HumanoidRootPart").Position
local angle = math.atan2(mousePosition - characterPosition.X, characterPosition.Z - characterPosition.Z)
local cameraRotation = workspace.CurrentCamera.CFrame
local cameraLookUpAngle = cameraRotation:ToEulerAnglesYXZ()
local clampedAngle = math.clamp(cameraLookUpAngle, -maxLookUpAngle, 0)
local rotation = CFrame.Angles(clampedAngle, angle, 0)
character:SetPrimaryPartCFrame(character.PrimaryPart.CFrame:lerp(rotation, rotationSpeed))
end
game:GetService("RunService").RenderStepped:Connect(RotateCharacter)
try this code, it checks if humroot exists
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.Camera
local rotationSpeed = 5
local maxLookUpAngle = math.rad(80)
local function RotateCharacter()
local mousePosition = player:GetMouse().X
if character:FindFirstChild("HumanoidRootPart") then
local characterPosition = character:WaitForChild("HumanoidRootPart").Position
local angle = math.atan2(mousePosition - characterPosition.X, characterPosition.Z - characterPosition.Z)
local cameraRotation = workspace.CurrentCamera.CFrame
local cameraLookUpAngle = cameraRotation:ToEulerAnglesYXZ()
local clampedAngle = math.clamp(cameraLookUpAngle, -maxLookUpAngle, 0)
local rotation = CFrame.Angles(clampedAngle, angle, 0)
character:SetPrimaryPartCFrame(character.PrimaryPart.CFrame:lerp(rotation, rotationSpeed))
end
end
game:GetService("RunService").RenderStepped:Connect(RotateCharacter)
local characterPosition = character.PrimaryPart.Position