PlayerAdded not firing when player teleported

Hello, I’m working on a restart chat command that teleports every player back into another server (for update). Th issue is that once back in the game, PlayerAdded is not firing in that new server. I have no idea how to fix this, any help is appreciated:

Teleport script:

function ShutDown()
	if (#Players:GetPlayers() == 0) then
		return
	end

	if (game:GetService("RunService"):IsStudio()) then
		return
	end

	local m = Instance.new("Message")
	m.Text = "Rebooting servers for update. Please wait"
	m.Parent = workspace
	wait(3)
	local reservedServerCode = TeleportService:ReserveServer(game.PlaceId)

	for _,player in pairs(Players:GetPlayers()) do
		TeleportService:TeleportToPrivateServer(game.PlaceId, reservedServerCode, { player })
	end
	Players.PlayerAdded:connect(function(player)
		TeleportService:TeleportToPrivateServer(game.PlaceId, reservedServerCode, { player })
	end)
	while (#Players:GetPlayers() > 0) do
		wait(1)
	end	
end
2 Likes

Maybe because the player joined the server without being recognized as a “new player” There is an easy fix for this which is:

local function onPlayerAdded(player: Player)

end

for _, player in game.Players:GetPlayers() do
     onPlayerAdded(player)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)

But if I do this a player might end up with multiple of the same folders and values, no?

1 Like

No, this will ensure that any players who joined before the PlayerAdded event started will spawn the PlayerAdded function on them.

And playerAdded function will not fire for old players that joined before it got initallized, and looping throught players will not count new players

Yeah but let’s say somebody joins and then somebody else also joins later, the first player who would already have their values would get handled a second time because of that for loop. Right?

that for loop will happen only one time in the entire server

Ohhhh ok ok I think this could work let me test it…

Might have to add a wait(5) or something

No need to wait, it should be done right away without yields.

It’s not working, I added this, but it doesn’t work:

spawn(function()
	repeat wait(0.5) until #game.Players:GetPlayers() > 0
	warn("PlayerAdded")
	PlrAdded()
end)

The warning is not printed

You’re over complicating this, just copy my code and put it wherever you want but use your PlrAdded function

That’s what I did at first, but it didn’t do anything :confused:

image

Can you send a full screenshot of the code and explorer tab with the script? Just making sure that the code would work in general.

How about just swapping these around…

Players.PlayerAdded:Connect(function(player)
     TeleportService:TeleportToPrivateServer(game.PlaceId, reservedServerCode, {player})
end)

for _, player in pairs(Players:GetPlayers()) do
     TeleportService:TeleportToPrivateServer(game.PlaceId, reservedServerCode, {player})
end
local function PlrAdded(player)
    if player:FindFirstChild('Loaded') then return; end

    Instance.new('BoolValue', player).Name = 'Loaded';

    Datastore.LoadData(player);
end

game:GetService('Players').PlayerAdded:Connect(PlrAdded);

for _,player in pairs(game:GetService('Players'):GetPlayers()) do
    PlrAdded(player);
end

The way you have it set up will iterate through every player, every time someone joins the server. This way is a lot cleaner.

Still the same, I don’t know why. It should work…

This neither, I really have no idea.

image

image

I figured that the script doesn’t run at all (why and how to fix it?)