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)
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)
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)
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)
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.