repeat wait() until char.Head.Neck
This is useless, you can remove it.
The FindFirstChild
is there to prevent the entire thing to error and make it return nil if it doesn’t exist, hence why we check after executing the function if not Neck then return end
.
Yeah i’ve already removed it, incase im missing anything btw, here’s the rest of the code:
local CamHandling = {}
local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local MouseFilter = require(script.Parent.MouseFilter)
local db = false
local look = true
local Character = Player.Character or Player.CharacterAdded:wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local UpperTorso = Character.UpperTorso
local NeckC0 = CFrame.new()
local CFNew, CFAng = CFrame.new, CFrame.Angles
local asin = math.asin
Camera.FieldOfView = 90
local function GetNeck(Character)
if Character:FindFirstChildWhichIsA("Humanoid").RigType == Enum.HumanoidRigType.R15 and Character:FindFirstChild("Head") then
return Character.Head:FindFirstChild("Neck")
elseif Character:FindFirstChildWhichIsA("Humanoid").RigType == Enum.HumanoidRigType.R6 and Character:FindFirstChild("Torso") then
return Character.Torso:FindFirstChild("Neck")
end
end
Player.CharacterAdded:Connect(function(char)
look = true
end)
function CamHandling:InputUpdate()
if look == true then
local Neck = GetNeck(Character)
if not Neck then return end
local YOffset = Neck.C0.Y
--CAMERA LOOK
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
if Player.Character.Humanoid.Health <= 0 then
look = false
Neck.C0 = NeckC0
end
if look == true then
game.ReplicatedStorage.Look:FireServer(Neck.C0)
end
end
end
return CamHandling
--The inputupdate function is fired from a seperate script which happens when userinputservice.inputchanged event is fired
(Neck item isn’t found within the player’s character)
Is your game R6 or R15, or even both? Because
local UpperTorso = Character.UpperTorso
will break the entire script if the user is R6.
It’s fully R15, just checked now, i’ll remove all the R6 side of the code
So, I tried your code on my side and it works perfectly fine. I get no errors at all. Could it be that you are in Team Create? If yes, check if Collaborative Editing is on, because it is, then there might be a chance that you are never using the newest version of the script since you never committed the changes. It does end up breaking after I reset because the script is still using the old variables, so I changed your CharacterAdded
to update all Variables, and also listen to when the Humanoid dies to set look
to false
. By doing this I also fixed the issue that after resetting the head no longer rotates.
Player.CharacterAdded:Connect(function(char)
look = true
Character = char
Root = char:WaitForChild("HumanoidRootPart")
UpperTorso = char:WaitForChild("UpperTorso")
local con
con = char.Humanoid.Died:Connect(function()
look = false
con:Disconnect()
end)
end)
This did the trick, it was a problem with the variables being old. Thank you for all the help!