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
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)
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?
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.