Shutdown All Servers

Good Afternoon,

I am trying to make admin settings for my game, I am trying to shutdown the servers with a button

What I tried is Remote Event, Bindable Event, And Remote Function,

But All That Didn’t Work For Some Reason

What I did:

Local Script From The Button

local event = game.ReplicatedStorage.ShutdownServer

script.Parent.MouseButton1Click:Connect(function()
	event:FireServer()
	print("Server Is fired | Button")
end)

Script In ServerScriptServer

local server = game.ReplicatedStorage.ShutdownServer

server.OnServerEvent:Connect(function()
	print("Server Is Fired")
	while wait(1) do
		for i,v in pairs(game.Players:GetPlayers()) do
			v:Kick("Server Has Been Shutdown by the Moderator")
		end
	end
end)

And A Remote Event In Replicated Storage

Hope You Could Help Me

Its a bit hard to fully help you since you dont provide that much info + both scripts are the same?

But if youre having problems with the server part. Then heres an easy way of shutting down a server with a Remote Event :slight_smile:




local Event = game.ReplicatedStorage.ShutdownServer


Event.OnServerEvent:Connect(function(Caller)
	
	for _, Plr in game.Players:GetPlayers() do
		Plr:Kick("Server shutdown.")
	end
end)
2 Likes

Thanks For Reminding Me I will Edit it to the new code

The code Works But I want it to work on every server that is opened

Youd have to use messaging service for this. Heres an example i put togheter :slight_smile:

Thought be aware that the message api has limits. Heres the docs MessagingService | Roblox Creator Documentation

Subscribe to the topic “Global-Shutdown”. Whenever you call(Or should we say publish) to this topic. The bound callback will be triggered on all servers and in the end kick all the players.

First code


local Api = game:GetService("MessagingService")



local Sucess,Err = pcall(Api.SubscribeAsync,Api,"Global-Shutdown",function()

	for _, Plr in game.Players:GetPlayers() do
		Plr:Kick("Server shutdown.")
	end

end)

if Sucess then
	warn("Sucessfully subscribed to topic")
else
	warn("An error occured at subscribing to topic:"  ..Err)
end

Remote event code

local Event = game.ReplicatedStorage.ShutdownServer




Event.OnServerEvent:Connect(function(Caller)
	Api:PublishAsync("Global-Shutdown","")
end)

where do I put the first code in

You can place it in a separate script in server script service. Or you could if you want put it inside your script that handles remote events. Though id recommended putting it in a separate script to keep things organized :slight_smile:

1 Like

The Script Have Worked, Thanks

And I want to know, does it work if I tween a GUI saying goodbye before it kicks

1 Like

Sure can. Heres a rough example:

local SomeRemoteThatFiresWhenServerShutdownHappens = game.ReplicatedStorage.Event


game:BindToClose(function()
	
	SomeRemoteThatFiresWhenServerShutdownHappens:FireAllClients()
	task.wait(5) -- // Your desried amount before the server shutsdown.
	
end)
1 Like

may I know what is bindtoclose

1 Like

Ah wait my bad lol. I by accident mixed up stuff. ignore that lol. Thats used when a developer decides to shutdown all servers. It fires when the server is about to shutdown.

Haha sorry my bad once again. I mixed some stuff up.

Heres the RIGHT code this time XD

local SomeRemoteThatFiresWhenServerShutsdown = game.ReplicatedStorage.Event

local Sucess,Err = pcall(Api.SubscribeAsync,Api,"Global-Shutdown",function()

	for _, Plr in game.Players:GetPlayers() do
		task.spawn(function()
			SomeRemoteThatFiresWhenServerShutsdown:FireAllClients() 
			task.wait(5) -- // Wait 5 seconds before kicking plrs 
			Plr:Kick("Server shutdown.")
		end)
	end

end)

it’s ok lol, u helped me a lot, thanks

1 Like

Np glad i could help out :grin:


Although this is a solution that works, using this is not a good idea.

You should expect that your remote will be fired by an exploiter at any time, with any arguments to the server function. This means that any exploiter who uses a program like RemoteSpy can fire that remote and shut down all of your servers.

A solution could be to make sure the Caller.UserId is your own ID or matches to an admin list you made.

2 Likes

that is a great idea ngl

so if I am going to use this I am going to use if statements right?

Here’s @Herobrinekd1’s original code:

local Event = game.ReplicatedStorage.ShutdownServer

Event.OnServerEvent:Connect(function(Caller)
	Api:PublishAsync("Global-Shutdown","")
end)

I don’t know if this is the best way to use that MessagingService::PublishAsync function, but here’s a good start if you want to modify it to be more secure:

local Event = game.ReplicatedStorage.ShutdownServer

Event.OnServerEvent:Connect(function(Caller)
    if Caller.UserId == game.CreatorId then
    	Api:PublishAsync("Global-Shutdown","")
    end
end)
2 Likes

Wow, Thanks @GFink and @Herobrinekd1 you helped me a lot + I learned a lot to, Big Thanks to you guys

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.