Somehow the script reads the humanoid root part only once

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:

22.760250091552734, 3.135000228881836, -1.2930774688720703 (x699)
1 Like

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)

oh i forgot about that, add a wait() instead of true

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

The script is running earlier than when the character spawns so change WaitForChild into FindFirstChild

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.