Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check if something matches in a table?

I know how to filter through a tables contents with a loop BUT I am trying to figure out how I will do something like make a WhiteList

What i wanted to do was Destroy a script completely if the user's name wasn't on the WhiteList so therefore they wouldn't be able to use it

This is what i've done so far

local player = game.Players.LocalPlayer
local WhiteList = {player, user, names, here}

for i,v in pairs(WhiteList) do 
	if player.Name  == v then 
	print('granted') 
	elseif player.Name ~= v then
	script:Destroy()
end
end

This works but it destroys the script even though it hasn't seen if the players name matches any of the other items in the script. How do i make it do it only after it's finished checking, and if it gets a match, it'll keep the script?

1 answer

✓ Accepted answer

It is better to use the UserId of the player than relying on the name of the player as you can change the name.

Also you do not need to use a for loop to check each player, instead use a key value pair to check if the key exists or nil if the key does not exist.

This is an example only:-

local player = game.Players.LocalPlayer
local lst = {[1540993] = true, [57651089] = true, [1234134] = true, [467223] = true}

if lst[player.UserId] then
	print('plr id found')
else
	print('plr id not found')
	script:Destroy()
end

Hope this helps.

1 comment

2

Telling the script to kill itself. How cruel.

This discussion is preserved as it was. New answers, comments, and votes are closed.