Server lock script who won't unlock/re-lock the server

Hello,

As the developer of a group, I needed a permanent server lock system who permanently let enter every player with rankID higher or equal 40 in the group, and who can be disabled with a command (/unlock) to let people under the rankID 40 to enter in the server, and who can be re-enable with an another command (/lock) to re-set the limit to the rankID 40.

The issue is when I use the unlock command it won’t work, but the lock command work perfectly:

I’ve set lock to false, then the unlock command worked perfectly, but the lock command stopped working, I’m kinda lost, that’s why I’m asking your help.

local minRank = 40
local groupID = 5912393
local lock = true

game:GetService("Players").PlayerAdded:Connect(function(plr)
    if lock == true and plr:getRankInGroup (groupID) < minRank then
        plr:kick ("The server is locked")
    end

    plr.Chatted:connect(function(msg)
        if plr:GetRankInGroup(groupID) >= minRank then
            if msg == "/lock" then
                lock = true and print("locked")
            end

            if msg == "/unlock" then
                lock = false and print("unlocked")
            end
        end
    end)
end)

The script don’t let people under the rankID 40, I’ve tested it.

2 Likes

Why are you using and…??? Just do it without using and?
print() returns absolutely nothing at all so true and print results in nil

Well I will re-test, without that.

Also, you can do this instead:

game:GetService("Players").PlayerAdded:Connect(function(plr)
    if lock and plr:getRankInGroup(groupID) < minRank then
        plr:Kick("The server is locked")
    else
        plr.Chatted:connect(function(msg)
            if plr:GetRankInGroup(groupID) >= minRank then
                if msg == "/lock" then
                    print("locked")
                    lock = true
                elseif msg == "/unlock" then
                    print("unlocked")
                    lock = false
                end
            end
        end)
    end
end)

Or better yet, listen to chat events from all players instead of just specific ones.

3 Likes

It work perfectly now, thank you so much for your help, I was totally lost.