Player and Group Whitelist

It keeps kicking me out even though I am whitelisted. How can I make this work for the players in the groups and the players in the whitelist?

local whitelist = {
[“privateschooI”] = true
}

game.Players.PlayerAdded:connect(function(p)
if not whitelist[p.Name] or not p:IsInGroup(5186633) or not p:IsInGroup(2820127) then
p:Kick(“You are banned from this game”)
end
end)

2 Likes

With the or statement, at least one of the conditions need to be true. It could be that you’re not in the whitelist, but you’re in the group. The whitelist condition will evaluate to true, which kicks you from the game. This goes for all of your conditions, pretty much

Your solution would be to make a variable you’d change if the player is found in the whitelist or the two groups:

game.Players.PlayerAdded:Connect(function(p)
    local whitelisted = false
    if whitelist[p.Name] or p:IsInGroup(5186633) or p:IsInGroup(2820127) then
        whitelisted = true
    end
    if not whitelisted then
       p:Kick("You are not whitelisted")
    end
end)

Also, please use codeblocks

2 Likes

You’re better off first searching for if a player is in the group and then do a loop for a table to check if the player is on the whitelist, also it is recommended to use playerIDs over names, as names can change and IDs cannot.

local Players = game:GetService("Players")

local whitelist = {
	0
	}

Players.PlayerAdded:connect(function(player)
	local isWhitelisted = false
	if not (player:IsInGroup(0) or player:IsInGroup(0)) then
		for i, v in pairs(whitelist) do
			if v == player.UserId then
				isWhitelisted = true
			end
		end
	else
		isWhitelisted = true
	end
	if isWhitelisted == false then
		player:Kick("Adios!")
	end
end)
2 Likes

Typically in cases like this I’ll get lazy or confused with what I’m doing and just end up creating a function for this that returns a boolean. It helps me to visualise what I’m doing as well, since I’m a very visual person and prefer readability over anything else.

Just a little tidbit that you should typically work in UserIds if you ever need a whitelist or something similar. UserIds are permanently tied to an account while a username is subject to change. You are able to get the UserId a username is associated with using GetUserIdFromNameAsync of the Players service. Similarly, GetNameFromUserIdAsync will get the current username associated with the UserId.

I don’t really encourage my way since it’s ugly and unnecessary but, again, visual.

local Players = game:GetService("Players")

local WHITELIST = {
    ["privateschool"] = true,
}

local function isWhitelisted(player)
    return WHITELIST[player.Name] or player:IsInGroup(5186633) or player:IsInGroup(2820127)
end

Players.PlayerAdded:Connect(function (player)
    if not isWhitelisted(player) then
        player:Kick("You are not allowed to play this game.")
    end
end)

You probably don’t need to use a kick script though. If you have the Game Access Permissions feature enabled, you can just grant access to specific user(s/groups). The play button will automatically be removed from the website and prevent non-privilege users from entering. You can even configure rank-based access. Here’s an example using a group I develop for:

3 Likes

Unless I’m mistaken on the purpose of the code, this should be as simple as changing the or statements to and. This will make it so that if all conditions are false (you’re not whitelisted, or in either of the groups) you will be kicked.

2 Likes