-
What do you want to achieve? Keep it simple and clear!
A soft shutdown mechanic, where once I shut down all servers, every player is teleported into a new server of the latest version. -
What is the issue? Include screenshots / videos if possible!
When a computer and mobile device are both playing on the same server, the script doesn’t teleport either player and the mobile player just gets disconnected.
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local RunService = game:GetService("RunService")
local ReserveCharacterPosition = true
local function CFrameToArray(CoordinateFrame: CFrame)
return {CoordinateFrame:GetComponents()}
end
local function ArrayToCFrame(a: {number})
return CFrame.new(table.unpack(a))
end
local function OnPlayerAdded(Player)
local TeleportData = Player:GetJoinData().TeleportData
if TeleportData and TeleportData.isSoftShutdown == true then
local CoordinateFrame = TeleportData.CharacterCFrames[tostring(Player.UserId)]
if ReserveCharacterPosition and CoordinateFrame then
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") :: BasePart
if not Player:HasAppearanceLoaded() then
Player.CharacterAppearanceLoaded:Wait()
end
task.wait(0.1)
HumanoidRootPart:PivotTo(ArrayToCFrame(CoordinateFrame))
end
end
end
local Connection = Players.PlayerAdded:Connect(OnPlayerAdded)
for _, Player in Players:GetPlayers() do
OnPlayerAdded(Player)
end
game:BindToClose(function()
if RunService:IsStudio() then return end
local CurrentPlayers = Players:GetPlayers()
if not CurrentPlayers[1] then return end
for _, Player in CurrentPlayers do
Player.PlayerGui.PlayerGuis.Shutdown.Enabled = true
end
task.wait(1)
local CharacterCFrames = {}
if ReserveCharacterPosition then
for _, Player in CurrentPlayers do
local Character = Player.Character
local HumanoidRootPart = Character and Character:FindFirstChild("HumanoidRootPart")
if HumanoidRootPart then
CharacterCFrames[tostring(Player.UserId)] = CFrameToArray(HumanoidRootPart.CFrame)
end
end
end
local TeleportOptions = Instance.new("TeleportOptions")
TeleportOptions:SetTeleportData({
isSoftShutdown = true,
CharacterCFrames = CharacterCFrames
})
local TeleportResult = TeleportService:TeleportPartyAsync(game.PlaceId, CurrentPlayers, TeleportOptions)
while Players:GetPlayers()[1] do
task.wait(1)
end
end)