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)
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
local Event = game.ReplicatedStorage.ShutdownServer
Event.OnServerEvent:Connect(function(Caller)
for _, Plr in game.Players:GetPlayers() do
Plr:Kick("Server shutdown.")
end
end)
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)
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
local SomeRemoteThatFiresWhenServerShutdownHappens = game.ReplicatedStorage.Event
game:BindToClose(function()
SomeRemoteThatFiresWhenServerShutdownHappens:FireAllClients()
task.wait(5) -- // Your desried amount before the server shutsdown.
end)
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)
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.
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)