Help with blacklist script

hi, could someone help why does this script not work. i want it to kick whenver a certain userid joins with the same reason listed in the table but for some reason it doesnt kick. and yes the userid is definitly right

local blacklisted = {
	{
		UserId  = 557094124,
		Reason = "test"
	}
}

game.Players.PlayerAdded:Connect(function(player)
	for i,v in pairs(blacklisted) do
		if table.find(blacklisted[i]["UserId"], player.UserId) then
			player:Kick([[
			User is blacklisted.
			
			Reason:]]
			..blacklisted[i]["Reason"])
		end
	end
end)

Might I suggest using ipairs? It goes in numerical order for the loop, instead of random.

Try implementing a couple of print statements in there to see what doesn’t work

local blacklisted = {
	{
		UserId  = 557094124,
		Reason = "test"
	}
}

print("This obviously works")

game.Players.PlayerAdded:Connect(function(player)
    print(player)
	for i,v in pairs(blacklisted) do
        local Check = table.find(blacklisted[i][UserId], player.UserId)

        print(Check)
		if Check then
			player:Kick([[
			User is blacklisted.
			
			Reason:]]
			..blacklisted[i]["Reason"])
        else
            warn("Blacklisted player not found")
		end
	end
end)

The directory isn’t indexed. You could do something like the following;

local blacklisted = {
	[1] = {
		UserId  = 557094124,
		Reason = "test"
	}
}

Also, there is no reason to use table.find() to check if the userId is equal. You can just do:

if blacklisted[i]["UserId"] == player.UserId then ...

However, I would recommend saving it in the following format;

local blacklisted = {
       [UserId] = "reason"
}

This way, you can access it like:

if blacklisted[player.UserId] then...
       player:Kick(blacklisted[player.UserId])

That is incorrect. Try:

if table.find(blacklisted[i]["UserId"]) == player.UserId then

You still need to do table.find(), because you cant check a table without it.

Unfortunately this results in an error:

image

table.find() requires 3 parameters, 2 of which are mandatory:

  • The table variable you’re searching for
  • (Optional) The table index you want it removed from
  • The actual value inside the table you want to check

I recommend writing it like this

local blacklist = {
	[500773613] = {
		Reason = "test"
	}
}


game.Players.PlayerAdded:Connect(function(player)
	for i,v in pairs(blacklist) do
		if player.UserId == i then
			player:Kick([[User is blacklistedReason: ]].. v["Reason"])
		end
	end
end)

hope this helps

You can check a table if you have the index at which the inner dictionary is stored.
E.g

if directory[index]["someInnerKey"] == "test" then..

That would work, as you are checking if the value of someInnerKey is equal to “test”, and in the blacklist case, that inner key would be UserId.

local blacklisted = {
	{["UserId"]  = 557094124, ["Reason"] = "Hacking"},
	{["UserId"]  = 123, ["Reason"] = "Test"}
}

game.Players.PlayerAdded:Connect(function(player)
	for _, entry in ipairs(blacklisted) do
		if entry["UserId"] == player.UserId then
			player:Kick([[
			User is blacklisted.
			
			Reason: ]]..entry["Reason"])
		end
	end
end)

You can’t use table.find() on a dictionary, instead what you can do is what I have done above.

The scripts in this topic are the most uselessly lengthiest scripts I’ve ever seen lol. I made a blacklist script with the same function but just extremely simplified:

local players = game:GetService("Players")
local banned = {
	[000000000] = "hacking lol"
}

players.PlayerAdded:Connect(function(player)
	if banned[player.UserId] then
		player:Kick(string.format("You are blacklisted.\n\nReason: %s.", banned[player.UserId]))
		return end
	
	-- do other stuff
end)

Screenshot 2021-12-09 at 8.42.20 pm

1 Like

There but idk it should work because my brain is size of 0.01.

local blacklisted = {
	{
		UserId  = 557094124,
		Reason = "test"
	}
}

local kickMessage = "You are blacklisted.\n\nReason: %q"

game:GetService("Players").PlayerAdded:Connect(function(player)
	for _,profile in pairs(blacklisted) do
		if profile.UserId == player.UserId then
			player:Kick(kickMessage:format(profile.Reason)
			break -- this will exit the loop because we no longer have to search for the player.
		end
	end
end)

table.find doesn’t work because you are dealing with a nested table. Using the pairs iterator function returns two values, the key and the value, usually known as a key-value pair. The key is the access identifier that “points to” the value. Because this is an array, the key would be an integer. However, because we don’t need to use it we use the _ underscore (just a common notation that the variable isn’t being used).

The value (in this case the profile variable) is the value the key points to. It is basically what you get when you do blacklisted[key].

  • Edit: Ignore my script and mark @voozy_v as the solution. I don’t know why, but I completely forgot about dictionaries. Lol. Anyways, his way is infinitely more efficient than mine since he uses a dictionary and is only performing a hash lookup. It is funny how our scripts ended up looking somewhat alike in function though.

Why? ipairs is slightly slower than pairs and there is no reason to iterate through it in order because the code doesn’t need it to be in order as long as it has the data.

No real idea, however, it can be more “ordered” in a sense.

Yes, but then you are just sacrificing efficiency for no reason at all.

True, however, whichever works.