Script doesn't work when using PlayerAdded?

Hello fellow developers, I’ve been trying to create a game and for the play screen I wanted the camera to be focused on something the instant they join. When I use Wait (5) it works but obviously thats not the exact time I join so theres still some time before it focuses on the part.

This is the script with the wait:

wait(5)

local cam = workspace.CurrentCamera
local part = workspace.Part

cam.CameraType = Enum.CameraType.Fixed
cam.Focus = part.CFrame

Heres the script with PlayerAdded:

game.Players.PlayerAdded:Connect(function()

local cam = workspace.CurrentCamera

local part = workspace.Part

cam.CameraType = Enum.CameraType.Fixed

cam.Focus = part.CFrame

end)

These are local scripts in StarterPlayerScripts btw if that has anything to do with it, in a summary I want every player when they join for their camera to be locked on a part like instantly, any help is appreciated, thank you and have a great day!

Since it’s a LocalScript, you don’t need to have a PlayerAdded event because you can reference the LocalPlayer as just game.Players.LocalPlayer.

This method works (tested). Although I can’t say it’s the most efficient method.

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

repeat task.wait() until game:IsLoaded() and player.Character

local focusPart = workspace:WaitForChild("Part")
camera.CameraType = Enum.CameraType.Fixed
camera.Focus = focusPart.CFrame

Thank you very much, it worked very well! I think I need to do a bit of research on “task”