Hello, I would like to know how can i lock and unlock a server?
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?
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)
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)
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
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)
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
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?
---- 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
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)
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.