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!