I’m making some sort of top-down camera and somehow, the function below only reads the humanoid root part once and keeps setting the camera’s cframe as the hrp’s original position.
local Player = game:GetService("Players")
local localplayer = Player.LocalPlayer
local Character = localplayer.Character or localplayer.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
function PositionCam() -- only
if HRP ~= nil then
Camera.CFrame = CFrame.new(HRP.CFrame.p + CamOffset, HRP.CFrame.p)
end
end
RunS.RenderStepped:Connect(PositionCam)
The print() output for the Humanoid root part’s Cframe position:
It might be reading it once because you didn’t put the script in a while loop.
Try this:
while true do
local Player = game:GetService("Players")
local localplayer = Player.LocalPlayer
local Character = localplayer.Character or localplayer.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
function PositionCam() -- only
if HRP ~= nil then
Camera.CFrame = CFrame.new(HRP.CFrame.p + CamOffset, HRP.CFrame.p)
end
end
RunS.RenderStepped:Connect(PositionCam)
end
This will crash the game as the loop is running too fast. Do this instead:
local Player = game:GetService("Players")
local localplayer = Player.LocalPlayer
function PositionCam() -- only
local Character = localplayer.Character or localplayer.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
if HRP ~= nil then
Camera.CFrame = CFrame.new(HRP.CFrame.p + CamOffset, HRP.CFrame.p)
end
end
RunS.RenderStepped:Connect(PositionCam)
This fixes it! But, I keep getting this warning and the function keeps going.
heres the warning:
Infinite yield possible on 'Workspace.jacobdavidaz12_Dev:WaitForChild("Torso")' - Studio
23:44:36.566 ▶ Infinite yield possible on 'Workspace.jacobdavidaz12_Dev:WaitForChild("HumanoidRootPart")' (x2) - Studio
23:44:37.031 Stack Begin - Studio
23:44:37.032 Script 'Players.jacobdavidaz12_Dev.PlayerScripts.CamScript', Line 22 - function PositionCam - Studio - CamScript:22
23:44:37.032 Stack End - Studio
23:44:37.165 Infinite yield possible on 'Workspace.jacobdavidaz12_Dev:WaitForChild("HumanoidRootPart")' - Studio
23:44:37.165 Stack Begin - Studio
23:44:37.165 Script 'Players.jacobdavidaz12_Dev.PlayerScripts.CamScript', Line 22 - function PositionCam - Studio - CamScript:22
23:44:37.165 Stack End - Studio