Slock Command Issue

Slock Command not working

Hey, fellow scripters!

I tried creating a !lock and !unlock script that would allow certain ranks to avoid it and that only certain ranks can run !lock and !unlock. After trying to test it, it seems not to work. After trying multiple times to fix it I have come to the conclusion I need to refer to the DevForum. So here I am. I included some instructions so you can understand what each thing is there for.

local Players = game:GetService("Players") -- Gets the service for players

local IsServerLocked = false -- Boolean on server start is set to false
local LockCommand, UnlockCommand = "!lock", "!unlock" -- Commands in string 
 format

Players.PlayerAdded:Connect(function(plr)


if plr:GetRankInGroup(10591136) <= 248 then
	if IsServerLocked == true then -- Checks if the server is locked
	plr:Kick("Training is currently undergoing. Please join when another session occurs.") -- Kicks the player off of the current server
	end
	
	Players.PlayerAdded:Connect(function(plr)
		if plr:GetRankInGroup(10591136) >= 237 then
plr.Chatted:Connect(function(Message) -- Event gets fired everytime the player has chatted
	-- Will use the message to see if it matches the command
	if string.lower(Message) == LockCommand then -- Checks if the lowercase version of the users message matches the lock command
		IsServerLocked = true -- Sets the boolean to true, locks the server
	elseif string.lower(Message) == UnlockCommand then -- Checks if the lowercase version of the message matches the unlock command
		IsServerLocked = false -- Sets the boolean to false, Unlocks the server
	end
end)
	end
1 Like

The main thing I notice about your code is that you listen for PlayerAdded twice, once in the first function but then again inside that function. There is no reason to do this and instead you can make some code such as the example I have listed below to achieve what you’re looking for.

---> services
local Players = game:GetService("Players")

---> variables
local GroupId = 10591136
local IsServerLocked = false
local LockCommand, UnlockCommand = "!lock", "!unlock"

---> main
Players.PlayerAdded:Connect(function(plr)
    -- kick players under the necessary rank if the server is locked
    if plr:GetRankInGroup(GroupId) <= 248 and IsServerLocked == true then
        plr:Kick("Training is currently undergoing. Please join when another session occurs.")
    end
    
    -- detect when players of a certain rank chat and if they do the command, update the variable accordingly
    if plr:GetRankInGroup(GroupId) >= 237 then
        plr.Chatted:Connect(function(message)
            if string.lower(message) == LockCommand then
                IsServerLocked = true
            elseif string.lower(message) == UnlockCommand then
                IsServerLocked = false
            end
        end)
    end
end)
3 Likes