As the title suggests I'm trying to fix my whitelist. It is pretty simple however my problem is I don't think I know how to compare table data properly:
local Whitelist = { "lol","leedle","omg","omgronny","xxxtentacion","HEREWEGOOO", "Radstar1","fajuro" } --If their name isn't on the list I want them to be kicked. game.Players.PlayerAdded:Connect(function(Player) for _,Name in pairs(Whitelist) do if string.match(Name,Player.Name) then return else Player:Kick("Not on the whitelist") print("Kicking "..Player.Name) end end end)
--It'll only work if the first returned value from the table is the player's name. Any suggestions?
Try this
local Whitelist = { "lol","leedle","omg","omgronny","xxxtentacion","HEREWEGOOO", "Radstar1","fajuro","dispeller" } --If their name isn't on the list I want them to be kicked. game.Players.PlayerAdded:Connect(function(Player) local shouldKick = true for i,v in pairs(Whitelist) do if Player.Name == v then shouldKick = false end end if shouldKick then Player:Kick("Sorry, you are not whitelisted!") end end)
This might be a better whitelist though, because people can change their username...
local Whitelist = {1, 2393433, 193694100} --If their UserID isn't on the list I want them to be kicked. game.Players.PlayerAdded:Connect(function(Player) local shouldKick = true for i,v in pairs(Whitelist) do if Player.UserId == v then shouldKick = false end end if shouldKick then Player:Kick("Sorry, you are not whitelisted!") end end)
Or if you still want to be ok with players changing their username, but dislike having to use User IDs instead of names, you could do this:
local Whitelist = {"lol","leedle","omg","omgronny","xxxtentacion","HEREWEGOOO", "Radstar1","fajuro","dispeller"} --If they are not on the list I want them to be kicked. game.Players.PlayerAdded:Connect(function(Player) local shouldKick = true for i,v in pairs(Whitelist) do if Player.UserId == game.Players:GetUserIdFromNameAsync(v) then shouldKick = false end end if shouldKick then Player:Kick("Sorry, you are not whitelisted!") end end)
You really are overcomplicating a simple thing. Use a key value pair so you can index the table insted of looping a checking each indavidual index in the table. It may also be better to use a datastore for this list as it would not require you to upate the code at all.
As dispeller said using the players id is better due to the fact that players are able to change their name. An id is saf to use as it will never change.
A quick example:-
local plrWhitelist = {} -- add data format string id = player name plrWhitelist['2393433'] = 'Radstar1' -- note the name is just a place holder we will not use it game.Players.PlayerAdded:Connect(function(plr) if not plrWhitelist[tostring(plr.UserId)] then -- not found in list plr:Kick('Not on the whitelist') end -- other setup end)
I hope this helps.