I am making a camera controller and I stumbled against a problem where the character is not being found. I have seen this problem before but I forgot what the solution is.
Here’s the cameracontroller:
(uses knit)
local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local CameraController = Knit.CreateController{
Name = "CameraController"
}
local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Settings = require(script.Settings)
local RunService = game:GetService("RunService")
local function Update()
if not Player.Character or Player.Character:FindFirstChild("HumanoidRootPart") then
warn("Character not found: Camera Controller")
return
end
local Root = Player.Character.HumanoidRootPart
Camera.CFrame = CFrame.new(Root.Position) * Settings.CameraAngle
end
function CameraController:KnitInit()
print(script.Name.." Initilized.")
end
function CameraController:KnitStart()
print(script.Name.." Started.")
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, Update) -- bind
end
return CameraController
Output:
06:52:21.811 Character not found: Camera Controller (x260)
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local CameraController = Knit.CreateController{
Name = "CameraController"
}
local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Character = Player.Character or Player.CharacterAdded:Wait()
local Settings = require(script.Settings)
local RunService = game:GetService("RunService")
local function Update()
if not Character or Character:FindFirstChild("HumanoidRootPart") then
warn("Character not found: Camera Controller")
return
end
local Root = Player.Character.HumanoidRootPart
Camera.CFrame = CFrame.new(Root.Position) * Settings.CameraAngle
end
function CameraController:KnitInit()
print(script.Name.." Initilized.")
end
function CameraController:KnitStart()
print(script.Name.." Started.")
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, Update) -- bind
end
return CameraController
it prints out the same thing. PS: I tried using Player.CharacterAdded:Wait() and it just yields the script infinitely
In Update() the condition is stopping execution. Translated to a sentence: If there is no character or if there is humanoid root part, run the do-block.
I think you would be fine by defining character and humanoid at the top too, and then simply calling a function to update with new character once player respawns.
Can you try fixing the script? I fixed it by removing the if statement, even though I didn’t think that was a good idea.
How would you fix it better?
local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local CameraController = Knit.CreateController{
Name = "CameraController"
}
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Settings = require(script.Settings)
local function Update()
--if not Player.Character or Player.Character:FindFirstChild("HumanoidRootPart") then
-- warn("Character not found:: "..Player.Name)
-- return
--end
local Root = Player.Character.HumanoidRootPart
local CameraCF = (Settings.CameraPosition * Settings.CameraOffset)
Camera.CFrame = CFrame.new(Root.Position) * CameraCF
end
function CameraController:KnitInit()
print(script.Name.." Initilized.")
end
function CameraController:KnitStart()
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, Update)
end
return CameraController
Sure, I’m on a phone, so it’s much harder to type. One way would simply be to add ‘not’ right before the FindFirstChild(), in the sense of “if there is no character or no root part, warn.
This way is not bad, but it does iterate through parts of the character until it finds the root part every frame.
Alternatively, you can have a function like
local character: Model
local rootPart: Part
local function AddNewCharacter()
character = Player.Character or Player.CharacterAdded:Wait()
rootPart = character:WaitForChild(“HumanoidRootPart”)
end
In the update function, you can then check for the existence of hrp and character.
if not character and not rootPart then
return;
end
On respawn, the update function can keep running and the components will simply be updated. Just in case, you can also set character and rootPart to nil moments before assigning new ones, which will make the Update function not continue.