How can I lock and unlock a server?

Hello, I would like to know how can i lock and unlock a server? :scream:
Do any of you have any code examples or something to learn

What happens is that I need to make when the player writes in the chat /.lock the server is locked and when the player writes /.unlock the server is unlock, how can I do this? :scream:

1 Like

You can hook the PlayerAdded event on a boolean value.

2 Likes

You can use

game.Players.PlayerAdded:Connect(function(player)
--check if server is locked

if serverLocked then
player:Kick("Server is currently locked!")
else
--server is not locked
end


end)
6 Likes
local admins = {
	1739689185 -- you
}

local locked = false

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	if (table.find(admins, player.UserId)) then
		-- admins can join regardless if server is locked or not.
		print("an admin player: "..player.Name..", joined.")
		player.Chatted:Connect(function(msg)
			if (msg == "/.lock") then
				print("locking server.")
				locked = true
			elseif (msg == "/.unlock") then
				print("unlocking server.")
				locked = false
			end
		end)
	elseif (locked) then
		-- non-admins can't join a locked server.
		print(player.Name.." attempted to join a locked server.")
		player:Kick("This server is locked.")
	else
		-- if player is not an admin and server is not locked, do nothing.
		print("non-admin player: "..player.Name.." joined.")
	end
end)
5 Likes

Thanks, how could I implement this in my code?

if Player.TeamColor == BrickColor.new("Lapis") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.lock' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("Server locked")
					-- code to locked here:
					serverLocked = true -- something like this?
				end
			end
		end
2 Likes

You could just do .lock, the Player2.Name isn’t exactly necessary unless if you want to kick/ban the Player

And assuming serverLocked is a BoolValue, you can check for the Changed property I believe if the server is locked or not (Then decide whether to lock it from new players joining or not)

1 Like

Check @blokav 's code, he’s supplied the full thing you’ll need, try and read and learn from it!

1 Like

Hello, I just tested your code, when I am in the game and I write the chat command the server locks and then kicks but the problem is that when my friend re-enters the server it does not kick him… i am not sure what’s wrong :frowning:

script command:

-- Locked/Unlocked server
		if (message == ".lock") then
			print("Looking server...")
			wait(3)
			locked = true
		elseif (message == ".unlock") then
			print("Unlocking server...")
			wait(3)
			locked = false
		elseif (locked) then
			-- non-admins can't join a locked server.
			print(Player.Name.." attempted to join a locked server.")
			Player:Kick("This server is locked.")
		else
			-- if player it not an admin and server is not locked, do nothing.
			print("non-admin player: "..Player.Name.." Joined.")
		end
	end)
end)

script PlayerAdded:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	
	-- Lock server & admin
	if (table.find(admins, Player.UserId)) then
		-- admins can join regardless if server is locked or not.
		print("an admin player: "..Player.Name..", joined.")
	end

When my friend joins the game the console show this print:
print("non-admin player: "..Player.Name.." Joined.")
Only my id is in the admins table, my friend’s id is not in the table, what is wrong? :frowning:

Sorry for the inconvenience

You need to check the ā€œlockedā€ variable in the PlayerAdded event, not the chat event.

Oh i see… like this?

---- ADMINS
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	
	-- Lock server & admin
	if (table.find(admins, Player.UserId)) then
		-- admins can join regardless if server is locked or not.
		print("an admin player: "..Player.Name..", joined.")
	end
	
	if (locked) then
		-- non-admins can't join a locked server.
		print(Player.Name.." attempted to join a locked server.")
		Player:Kick("This server is locked.")
	else
		-- if player it not an admin and server is not locked, do nothing.
		print("non-admin player: "..Player.Name.." Joined.")
	end

Yes, though that code would kick admins too if the server was locked.
Change it to elseif (locked) then

---- ADMINS
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	
	-- Lock server & admin
	if (table.find(admins, Player.UserId)) then
		-- admins can join regardless if server is locked or not.
		print("an admin player: "..Player.Name..", joined.")
	elseif (locked) then
		-- non-admins can't join a locked server.
		print(Player.Name.." attempted to join a locked server.")
		Player:Kick("This server is locked.")
	else
		-- if player it not an admin and server is not locked, do nothing.
		print("non-admin player: "..Player.Name.." Joined.")
	end

It does not work, nothing happens when I put the command … it only appears in the f9 console of the loocking game and then nothing happens :frowning:

full code:

-- DataStoreService
local DS = game:GetService("DataStoreService")
local BanStore = DS:GetDataStore("Ban")

-- Admins user id and lock
local admins = {
	1739689185 -- you
}

local locked = false

---- ADMINS
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	
	-- Lock server & admin
	-- Lock server & admin
	if (table.find(admins, Player.UserId)) then
		-- admins can join regardless if server is locked or not.
		print("an admin player: "..Player.Name..", joined.")
	elseif (locked) then
		-- non-admins can't join a locked server.
		print(Player.Name.." attempted to join a locked server.")
		Player:Kick("This server is locked.")
	else
		-- if player it not an admin and server is not locked, do nothing.
		print("non-admin player: "..Player.Name.." Joined.")
	end

	-- Kicked player
	if BanStore:GetAsync(Player.UserId) and BanStore:GetAsync(Player.UserId) == true then
		Player:Kick("You have been banned!")
	end

	Player.Chatted:Connect(function(message)

		--UnBan
		if string.find(message, ".unban ") then
			local newStr, replaced = string.gsub(message, ".unban ", "")
			local id = 1
			pcall(function ()
				id = Players:GetUserIdFromNameAsync(newStr)
			end)

			if id ~= 1 then
				BanStore:SetAsync(id, false)
			end
		end 

		-- Kick player message
		if Player.TeamColor == BrickColor.new("Lapis") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.kick ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					Player2:Kick("You have been kicked by the owner.")
				end
			end
		end

		-- warn player message
		if Player.TeamColor == BrickColor.new("Lapis") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.warn ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("The administrator has given you a warning")
					-- code to warning here:

				end
			end
		end

		-- ban player message
		if Player.TeamColor == BrickColor.new("Lapis") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.ban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been banned by the administrator")
					BanStore:SetAsync(Player2.UserId,true)
					Player2:Kick()
				elseif message == '.unban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been unbanned by the administrator")
					BanStore:SetAsync(Player2.UserId,false)
				end
			end
		end
		
		-- Locked/Unlocked server
		if (message == ".lock") then
			print("Looking server...")
			wait(3)
			locked = true
		elseif (message == ".unlock") then
			print("Unlocking server...")
			wait(3)
			locked = false
		end
	end)
end)

You should first check if the person is NOT an admin and the server is locked then it kicks them instead of whatever you’re doing right now.

-- DataStoreService
local DS = game:GetService("DataStoreService")
local BanStore = DS:GetDataStore("Ban")

-- Admins user id and lock
local admins = {
	1739689185 -- you
}

local locked = false

---- ADMINS
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	
	-- Lock server & admin
	if not table.find(admins, Player.UserId) and locked then
		print(Player.Name.." attempted to join a locked server.")
		Player:Kick("This server is locked.")
	end

	-- Kicked player
	if BanStore:GetAsync(Player.UserId) and BanStore:GetAsync(Player.UserId) == true then
		Player:Kick("You have been banned!")
	end

	Player.Chatted:Connect(function(message)

		--UnBan
		if string.find(message, ".unban ") then
			local newStr, replaced = string.gsub(message, ".unban ", "")
			local id = 1
			pcall(function ()
				id = Players:GetUserIdFromNameAsync(newStr)
			end)

			if id ~= 1 then
				BanStore:SetAsync(id, false)
			end
		end 

		-- Kick player message
		if Player.TeamColor == BrickColor.new("Lapis") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.kick ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					Player2:Kick("You have been kicked by the owner.")
				end
			end
		end

		-- warn player message
		if Player.TeamColor == BrickColor.new("Lapis") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.warn ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("The administrator has given you a warning")
					-- code to warning here:

				end
			end
		end

		-- ban player message
		if Player.TeamColor == BrickColor.new("Lapis") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.ban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been banned by the administrator")
					BanStore:SetAsync(Player2.UserId,true)
					Player2:Kick()
				elseif message == '.unban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been unbanned by the administrator")
					BanStore:SetAsync(Player2.UserId,false)
				end
			end
		end
		
		-- Locked/Unlocked server
		if (message == ".lock") then
			print("Looking server...")
			wait(3)
			locked = true
		elseif (message == ".unlock") then
			print("Unlocking server...")
			wait(3)
			locked = false
		end
	end)
end)
1 Like

Will this only kick players who are not admins? admins could join? :scream:

Yeah ofcourse, try it out and mark it as a solution if it worked!

if status == "Locked" then
game.Players.PlayerAdded:Connect(function(player)
if player then
player:Kick("The server is locked!")
end
end)

local Locked = game.ReplicatedStorage.YourValueNameHere

game.Players.PlayerAdded:Connect(function(plr)
if Locked = false then
print(ā€œnot lockedā€)
elseif Locked == true then
plr:Kick(ā€œServer lockedā€)
end)
end

I am not sure about the ends so write it by yourself, it is just to get you started.

2 Likes

Oh it’s not working properly :frowning:

image