Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't this serverlock script work(It is not the variables).?

I was wondering why it doesnt server lock if i type in the command? Note this is not a free model i wrote playername for convenience and because i wasn't sure if i was still gonna use player name i just wanted to be specific instead of people asking me why u didnt put in the variable. Please don't label this as a free model I really need help because I've been trying and I can't seem to get it to work.

local slock = false
local admin = "playername"
local Player = game:GetService("Players")


game.Players.PlayerAdded:Connect(function(plr)
    if plr.Name == admin and slock == true then
      plr:kick("Server is Locked")
      print("locked")
       else 
        if slock == false
        then
            print("unlocked")
        wait()
        end
        end
              plr.Chatted:Connect(function(Message)
             if string.lower(string.sub(Message, 1 , 5)) == "slock" and Player.Name == admin then
            slock = true
        else
            if string.lower(string.sub(Message, 1 , 6)) == "unslock" and Player.Name == admin then
            slock = false
            end
          end 
    end)
    end)

1 answer

✓ Accepted answer

Good afternoon!

I'm not a big fan of how that script works, so I've whipped up my own to share with you!


local slock = false --Default. Server is not locked.
local admins = {37040233, 34215} --Here I created a table with UserIds. Enter all of the people who you want to be able to use the slock and unslock commands here. I prefer IDs over usernames since you can change your username, but you can't change your userId.

local function checkIfAdmin(player) --This function will check to see if the player in question is an admin
	for i, v in pairs(admins) do --Goes through all the admins.
		if player.UserId == v then --See if we find a match between the players id and a value in our admins table.
			return true --Returning true. The user in question is an admin
		else --Otherwise, 
			return false --Return false. The user in questions is NOT an admin
		end
	end
end


game.Players.PlayerAdded:Connect(function(plr) --Runs upon a new player entering the game
	if not checkIfAdmin(plr) and slock then --if the server is locked, and the player is not an admin:
		plr:Kick("The server is locked!") --Kick the player. You can change the text inside :Kick()
	end
	
	if checkIfAdmin(plr) then -- If the player who entered is an admin
		plr.Chatted:Connect(function(Message) --Whenever the admin chats, run the following:
			if string.lower(string.sub(Message, 1 , 5)) == "slock" then --If the first five letters of the admins chat is equal to "slock", then:
				print("Server locked")
				slock = true -- slock is now set to true. Players who join who are not admins will be kicked!
			elseif string.lower(string.sub(Message, 1 , 7)) == "unslock" then--If the first seven letters of the admins chat is euqal to "unslock", then:
				print("Server unlocked")
				slock = false --We re-open the servers up to everyone!
			end
		end)
	end
end)

The code without my comments(a lot cleaner, haha:)):

local slock = false
local admins = {37040233}

local function checkIfAdmin(player)
	for i, v in pairs(admins) do
		if player.UserId == v then
			return true
		else
			return false
		end
	end
end
game.Players.PlayerAdded:Connect(function(plr)
	if not checkIfAdmin(plr) and slock then
		plr:Kick()
	end
	if checkIfAdmin(plr) then
		plr.Chatted:Connect(function(Message)
			if string.lower(string.sub(Message, 1 , 5)) == "slock" then
				print("Server locked")
				slock = true
			elseif string.lower(string.sub(Message, 1 , 7)) == "unslock" then
				print("Server unlocked")
				slock = false
			end
		end)
	end
end)


Be sure to let me know if you encounter any issues with the code!

Best regards,

thetacah

This discussion is preserved as it was. New answers, comments, and votes are closed.