"Player Added" function not working as expected

Hello! I have made a simple camera system that shows my title screen at the start of the game using this code. It should be disabled when pressing the start button.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")

local camera = workspace.Camera
local part = workspace.CameraPart

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Visible = false
	script.Parent.Sound.TimePosition = 0
	script.Parent.Sound.Playing = true
	camera.CameraType = Enum.CameraType.Custom
	camera.CFrame = part.CFrame:Destroy()
end)


while true do
    task.wait(1)
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = part.CFrame
end

Obviously using while true do repeats it every second. So I try to add a Player Added function instead so it only does it once like this:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")

local camera = workspace.Camera
local part = workspace.CameraPart

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Visible = false
	script.Parent.Sound.TimePosition = 0
	script.Parent.Sound.Playing = true
	camera.CameraType = Enum.CameraType.Custom
end)


game.Players.PlayerAdded:Connect(function(plr)
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = part.CFrame

end)

And… Nothing. The camera doesn’t work and I don’t get a single error. Does anyone have any ideas as to why it is not triggering?

Here is my structure if it helps.

Screenshot 2023-07-12 084812

This is because this is a local script, to fix this you can use a remote event, or use the changed event by using values instead of using loops.

Add wait() in the beginning of PlayerAdded event

or try using RemoteEvents because I think PlayerAdded is not working in Local Scripts:

Tried a remote event out. Didn’t do anything :confused:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = part.CFrame
		print("Player Added!")
	end)
end)

Wait…
You do not even need PlayerAdded. LocalScript fires everything for each individual player who joined the game. Just write this in the beginning without PlayerAdded:

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = part.CFrame

1 Like

I had my head scratching for hours! Guess I was overthinking it. Laughed so hard when it worked. Thank you.

Here is the final code for anyone who finds this post:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")

local camera = workspace.Camera
local part = workspace.CameraPart

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Visible = false
	script.Parent.Sound.TimePosition = 0
	script.Parent.Sound.Playing = true
	camera.CameraType = Enum.CameraType.Custom
	camera.CFrame = part.CFrame:Destroy()
end)


task.wait()
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = part.CFrame

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