Problems with lock/unlock server script

Hello, I have a problem with my lock/unlock script, I don’t know what is wrong and I have been trying to solve it for 2 days :frowning:

image

Does anyone know how to fix this?

Script code:

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

Player.Chatted:Connect(function(message)

-- 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)

There is no error in the script and when I use the command “.lock” from my roblox player on the console (f9) it says “looking server” and nothing happens.

1 Like

What is the problem here exactly? I think it is doing what it should.

Have you tried to join from a second client after you chatted “.lock”, does that client get kicked?

It does not kick anyone :frowning: when I use the command only in the console it says “loocking server” and nothing else happens, nobody is kicked or anything

the problem is in the line 111

Well nobody should get kicked anyway, that’s what this script is doing.
It only locks the server, meaning that it only prevents new people from joining that server.
What exactly do you want to it to do when you type “.lock”.
Do you want to only lock the server to prevent new people from joining or kick every non-admin and prevent people from joining?

If you want to kick non-admins and prevent new people from joining, check out below what vParkuu posted.

2 Likes

Its because you put the Kick() just when a player joins
try this:

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

    Player.Chatted:Connect(function(message)
        -- Locked/Unlocked server
        if (message == ".lock") then
            print("Locking server...")
            wait(3)
            locked = true
            if not table.find(admins, Player.UserId) then
                Player:Kick()
            end
        elseif (message == ".unlock") then
            print("Unlocking server...")
            wait(3)
            locked = false
        end
    end)
end)
1 Like

Here’s a code that might work (insert a script into ServerScriptService and write this):

local GroupId = 12345 --REPLACE 12345 WITH YOUR GROUP ID
local MinimumRankToUseCommands = 254 --REPLACE 254 WITH THE MINIMUM RANK 

local MinimumRankToJoinSlocked = 10 --REPLACE 10 WITH THE MINIMUM RANK ID 
PLAYERS HAVE TO BE IN ORDER TO JOIN WHEN THE SERVER IS LOCKED
local ServerLockMessage = "The server is locked! Please try joining again later." -- 
REPLACE THE TEXT INSIDE OF THE "" WITH YOUR SERVER LOCK MESSAGE

local ServerLocked = false

game.Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
	if Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommands then
		if Message == "!slock" then
			ServerLocked = true
			
			local Players = game.Players:GetPlayers()
			
			for i = 1, #Players do
				local CurrentPlayer = Players[i]
				local ServerLockMessage = Player.PlayerGui.ServerLockGUI.ServerLockMessage
				
				ServerLockMessage.Visible = true
				ServerLockMessage.Text = "Server locked for ranks " .. MinimumRankToJoinSlocked .. " and lower."
				wait(5)
				ServerLockMessage.Visible = false
			end
			
		elseif Message == "!unslock" then
			ServerLocked = false
			
			local Players = game.Players:GetPlayers()
			
			for i = 1, #Players do
				local CurrentPlayer = Players[i]
				local ServerLockMessage = 
                Player.PlayerGui.ServerLockGUI.ServerLockMessage
				
				ServerLockMessage.Visible = true
				ServerLockMessage.Text = "Server Unlocked"
				wait(5)
				ServerLockMessage.Visible = false
			end
		end
	end
end)

if ServerLocked == true then
	if Player:GetRankInGroup(GroupId) < MinimumRankToJoinSlocked then
		Player:Kick(ServerLockMessage)
	  end
   end
end)
1 Like

This works! thanks a lot! :smiley: :smiley: :smiley: