Simple kick script is not working, why?

Hello. I’m trying to achieve a system that checks if a player is a certain user by checking their name on playerAdded, and kicking them if they are not from the table of player string names.

It is kicking me, even though I am on the table, why is this happening?

I’ve tried switching to userIds, not working? I do not know why.

local playerNames = {"user1","user2","user3"}

game.Players.PlayerAdded:Connect(function(plr)
	wait(5)
	if table.find(playerNames,plr.Name) then
		print(plr.Name .. " is a tester or is owner.")
	else
		wait(1)
		print(plr.Name .. " is not tester, kicking...")
		plr:Kick("You are not a tester; you do not have access to the game.")
	end
end)

It isn’t good a Good idea to be using their Names as they can easily change them and gain access to the game again Lose access to the game, use their UserId which is a permanent thing assigned to their account.

1 Like

You could try this:

-- try this
local Testers = {
    "User1",
    "User2",
    "User3"
}

game:GetService("Players").PlayerAdded:Connect(function(player)
    
    for i, tester in pairs(Testers) do
        if tester == player.Name then
            print("Player is a tester or owner")
        else
            player:Kick("Not a tester.")
        end
    end
end)
1 Like

use UserId.

local Testers = {
    paste your id here,
    paste your id here
}

game:GetService("Players").PlayerAdded:Connect(function(player)
    task.wait(5)
    for i, tester in pairs(Testers) do
        if tester == player.UserId then
            print("Player is a tester or owner")
        else
            player:Kick("Not a tester.")
        end
    end
end)
1 Like

Oh ok sorry! thanks for the tip

1 Like

Using table.find() works already, there is no reason to use a for loop as it already searches the Keys and Values.

Its basically table.find() with extra steps

1 Like

oh, i was like i saw - i corrected. Yes, it is, here the code

local Testers = {
    paste your id here,
    paste your id here
}

game:GetService("Players").PlayerAdded:Connect(function(player)
    task.wait(5)
    if table.Find(Testers, player.UserId) then
        print("Player is a tester")
    else
        player:Kick("Not a tester.")
    end
end)
1 Like

You dont need to add a wait to it lol. This should work.

local Players = game:GetService("Players")

local playerNamesTable = {"user1","user2","user3"}

Players.PlayerAdded:Connect(function(Player)
	if not table.find(playerNamesTable, Player.Name) then
		Player:Kick("You are not a tester; you do not have access to the game.")
	end
end)
1 Like

I reccomend to change it to userid

1 Like

Actually nvm, this system doesn’t appear to work, it would probably be best if you iterated through the table as for some reason, it returns nil instead of giving you the index.

1 Like

I recommend using collaboration and allowing your testers to play while keeping it private because otherwise if a random players try’s to play and they get kicked they will not want to return when the game is out.

2 Likes

Thank you; works perfectly. I’ll use userId more often in these scenarios now. Thank you for the help everybody.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.