How to shutdown server by script?

Hi,
Just a quick question. How can I shutdown server by a script? (I searched for the answer on DevForum and did not find it)

2 Likes

Kick all players using a loop and :Kick(), and the server stop.

4 Likes

Make sure to limit the max players to 0 when doing this otherwise a player might join after its running through all the active players. Joining a game and getting kicked straight away wouldnt be good.

I would reccommend moving players to another place instead, it makes everything go smoother if you are just doing an update.

13 Likes
while wait(1) do
    for i,v in pairs(game.Players:GetPlayers()) do
        v:Kick("The server is shutting down")
    end
end
9 Likes

@rtvr56565 has the right idea, but as @minimic2002 mentioned, there could be an instance where you run this code and a player joins at the right time, hence the server you wanted to shut down wouldnā€™t be.

A way around this that I would do is have a boolean which is set to true when you no longer want players joining the game. Add it to a connected function to the PlayerAdded event, if the boolean is true, kick the player. A usage and code example of this would be:

local PS = game:GetService("Players")

local canPlayerJoin = true

PS.PlayerAdded:Connect(function(player)
    if not canPlayerJoin then
        player:Kick("No future player can join the server!")
    end
end)

function ShutdownServer()
    canPlayerJoin = false

    for _, plr in ipairs(PS:GetPlayers()) do
        plr:Kick("Shutdown server!")
    end
end

Something like this, you would call ShutdownServer wherever you want the eventuality for the server to be shut down, the better option would have been something called :Shutdown() which is a part of the ā€˜DataModelā€™ instance but sadly you can only execute the function in the command line.

If you have any issues with the code or any questions, let me know! :slight_smile:

10 Likes

Hmm, yeah you can kick all the players in a loop until there are no players left though. But, if its for an update or something you can move players to a new server using teleport service by reserving a server and moving them to it.

5 Likes

If @CelestialHonour is right and the use case for this is for an update to a game, to have the players not ā€˜leaveā€™ the game so theyā€™re still playing you can do as he says, the code for this wouldnā€™t have to be changed much from my original post.

Instead of using :Kick() on the player, we would use :ReserveServer(placeId) to give ourselves an access code so that we can use :TeleportToPrivateServer() to teleport all players to that particular server.

The code for this would be something like:

local PS = game:GetService("Players")
local TPS = game:GetService("TeleportService")

local canPlayerJoin = true

PS.PlayerAdded:Connect(function(player)
    if not canPlayerJoin then
        player:Kick("No future player can join the server!")
    end
end)

function ShutdownServerForUpdate()
    canPlayerJoin = false

    local placeId = game.PlaceId
    local reservedServer = TPS:ReserveServer(placeId)
    TPS:TeleportToPrivateServer(placeId, reservedServer, PS:GetPlayers())
end

Never used TeleportService before so it might not be right, but looking at the documentation it should be, Iā€™ll link the functions mentioned if needed:

:ReserveServer()
https://developer.roblox.com/en-us/api-reference/function/TeleportService/ReserveServer

:TeleportToPrivateServer()
https://developer.roblox.com/en-us/api-reference/function/TeleportService/TeleportToPrivateServer

The only caveat with this method is, no new players will be able to join the server reserved, itā€™ll only be the players that existed in the server previous.

If thereā€™s problems with this code or if you have any more questions, donā€™t hesitate to ask. :grin:

5 Likes

Hi I know Iā€™m late but just to improve on this for anyone reading in the future thereā€™s no reason why you couldnā€™t connect the PlayerAdded signal to its function inside ShutdownServerForUpdate like this instead, removing the need for the boolean and making code a little cleaner

local PS = game:GetService("Players")
local TPS = game:GetService("TeleportService")

function ShutdownServerForUpdate()
	local reservedServer = TPS:ReserveServer(game.PlaceId)
	TPS:TeleportToPrivateServer(game.PlaceId, reservedServer, PS:GetPlayers())

	PS.PlayerAdded:Connect(function(player)
		player:Kick("No future player can join the server!")
	end)
end
5 Likes

This version teleports incoming players to the place the old players were just transported to.

local PS = game:GetService("Players")
local TPS = game:GetService("TeleportService")

function ShutdownServerForUpdate()
	local reservedServer = TPS:ReserveServer(game.PlaceId)
	TPS:TeleportToPrivateServer(game.PlaceId, reservedServer, PS:GetPlayers())
	PS.PlayerAdded:Connect(function(player)
		local PlayerList = {}
		PlayerList[#PlayerList + 1] = player
		TPS:TeleportToPrivateServer(game.PlaceId, reservedServer, PlayerList)
	end)
end
8 Likes

A very simple way to do it (can only be done on server script) is just to put game.Players:ClearAllChildren(). That will pop up a error saying ā€œRoblox has shut down this server for maintenanceā€ and the server will shut down.

3 Likes

The goal of shutting down the server is to transfer them to a new game, this would be frustrating for players. If you transfer them to a new server with the same people itā€™s like nothing changed.

2 Likes

It may be better to utilize the :Kick() function, but I guess that error msg is more bold.

4 Likes

Well, my method also allows the user to reconnect because it shows a reconnect button. That way they can keep playing on a different server.

2 Likes

My method autoreplaces players + new players in a server so the wonā€™t notice a thing, because they are with the same people with the same stats.

2 Likes