Shutting down a server using scripts

This seems like a pretty common topic for some reason. So I’m going to make a quick and easy to follow tutorial on how to shut down a server by a script.

Important note: This is heavily unrecommended for updates. Please use a soft shutdown system for restarting servers for an update.

First, of course, we want to get the Players service, just incase.

local Players = game:GetService("Players")

Then, we want to make sure that no new players can join.
To do this, we simply just write

Players.PlayerAdded:Connect(function(player)
	player:Kick("\nThis server has shutdown.")
end)

This function will take the player instance that the PlayerAdded event provides and runs the :Kick method. This method allows you to provide a string to the kick message, so when they get kicked, you can do something like provide a reason! In this case, the reason is that the server has shut down.

Now, we want to kick all the players that are currently in the server.
To do this, we use a for loop.

for _,player in pairs(Players:GetChildren()) do
	player:Kick("\nThis server has shutdown.")
end

We don’t need the index, so we replace i with _, and we can name the value player instead of v.
Then we use this for loop to go through the children of Players, and then kicks the players, with the same reason, individually.

Then, the server should automatically shutdown due to the absense of players!

Here is the full script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player:Kick("\nThis server has shutdown.")
end)

for _,player in pairs(Players:GetChildren()) do
	player:Kick("\nThis server has shutdown.")
end

I hope this tutorial has helped you, and for all the experienced users, made your jobs easier!

9 Likes

How does this shutdown a server efficiently? If anything you’ll probably lose players instead of gain.

Probably this guide is oriented towards those who want to completely shutdown their servers when a new update or change comes out for their game, sometimes Roblox default shutdown feature won’t work as expected.

Although, I don’t really recommend shutting down servers even when updates come out, that could lead to players loosing progress, getting mad or even data loss in worst cases. Instead find another method with a more soft approach like closing all the outdated servers to only allow new servers to be joined.

For player retention a Soft Shutdown (teleporting out of the game, then back to it) is what you want.

4 Likes

Achieve this with the BindToClose() function

1 Like

Pretty cool, but I hmmmmm my approach was more like:

When you update a game all the server that detect its version as “old” get closed, so no one can enter them. You won’t shutdown those servers but eventually since no one will join them they will get closed by time. Though about that because shutdowns may affect players specially in long games like boss fights or dungeons were you’re probably not going to reach out the end before 15, 30 or 60 minutes.

Even though you won’t kick players out of the game, you’re making them unintentionally leave the outdated server and join a new one just to see the new updates and because their server is that old that they will end alone eventually.

And how do you intend to do that? MaxPlayers is a read-only property. Developers cannot write to it.

Wouldn’t really call this an efficient shut down. What’s inefficient about the current shut down process and how does this code improve on that? These would be good points for the sake of comparison to understand your approach in writing this resource and why it’d be good to use it.

In any case, the engine already handles disconnecting clients when you use a shut down feature on the website so really you just need to invest time, if necessary, into shut down procedures specific to your experience. This includes data saving, “soft shut downs” or anything else you want to occur when a shut down is issued. BindToClose will help with that.

4 Likes

@HakaiShin_AOD Hey buddy you gonna shutdown the thread or not?

No no and no.

Please dont use Players:GetChildren() ever.

Always use Players:GetPlayers()