Hello! I’m trying to make it so the players head is following the players camera but I’m getting an error saying attempt to index nil with ‘WaitForChild’ on line 6.
local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character:WaitForChild("HumanoidRootPart")
local Neck = Character:FindFirstChild("Neck", true)
local YOffset = Neck.C0.Y
local CFNew, CFAng = CFrame.new, CFrame.Angles
local asin = math.asin
game:GetService("RunService").RenderStepped:Connect(function()
local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
if Neck then
if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
end
end
end)
while wait(2) do
game.ReplicatedStorage.Look:FireServer(Neck.C0)
end
local Camera = workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- if we don't find the character we wait for it to load
local Root = Character:WaitForChild("HumanoidRootPart")
local Neck = Character:FindFirstChild("Neck", true)
local YOffset = Neck.C0.Y
local CFNew, CFAng = CFrame.new, CFrame.Angles
local asin = math.asin
game:GetService("RunService").RenderStepped:Connect(function()
local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
if Neck then
if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
end
end
end)
while task.wait(2) do
game.ReplicatedStorage.Look:FireServer(Neck.C0)
end
game.ReplicatedStorage.Look.OnServerEvent:Connect(function(player, neckCFrame)
for key, value in pairs(game.Players:GetChildren()) do
if value ~= player and (value.Character.Head.Position - player.Character.Head.Position).Magnitude < 50 then
game.ReplicatedStorage.Look:FireClient(value, player, neckCFrame)
end
end
end)
Seems like it should all be working then. Unless you know of any errors on the client side of Look.OnServerEvent. Did you write these scripts? You are probably meant to put the first script into StarterCharacterScripts
local tweenService = game:GetService("TweenService")
local Camera = workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Root = Character:WaitForChild("HumanoidRootPart")
local Neck = Character:FindFirstChild("Neck", true)
local YOffset = Neck.C0.Y
local CFNew, CFAng = CFrame.new, CFrame.Angles
local asin = math.asin
game:GetService("RunService").RenderStepped:Connect(function()
local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
if Neck then
if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
end
end
end)
game.ReplicatedStorage.Look.OnClientEvent:Connect(function(otherPlayer, neckCFrame)
local Neck = otherPlayer.Character:FindFirstChild("Neck", true)
if Neck then
tweenService:Create(Neck, TweenInfo.new(.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {C0 = neckCFrame}):Play()
end
end)
while task.wait(1) do
game.ReplicatedStorage.Look:FireServer(Neck.C0)
end
serverscriptservice
game.ReplicatedStorage.Look.OnServerEvent:Connect(function(player, neckCFrame)
for key, value in pairs(game.Players:GetChildren()) do
if value ~= player and (value.Character.Head.Position - player.Character.Head.Position).Magnitude < 10 then
game.ReplicatedStorage.Look:FireClient(value, player, neckCFrame)
end
end
end)