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)
Kick all players using a loop and :Kick(), and the server stop.
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.
while wait(1) do
for i,v in pairs(game.Players:GetPlayers()) do
v:Kick("The server is shutting down")
end
end
@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!
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.
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.
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
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
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.
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.
It may be better to utilize the :Kick()
function, but I guess that error msg is more bold.
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.
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.