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

Why does this print even though its in the table?

This should should only print "yes" if its not in the blacklist table, but it prints "yes" for everything even if in the blacklist table.

local blacklist = {242230929, 320742188}

function ifAllowed(id)
	for i = 1, #blacklist do
		if blacklist[i] ~= id then
			return true
		else
			return false
		end
	end
end

game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		--print(msg)
		if checkRank(player) then
			if msg == "/skip" then
				songPlaying = false
				getSong()
			elseif string.sub(msg, 1, 7) == "/music " then
				local d = string.sub(msg, 8, string.len(msg))
				local validId = market:GetProductInfo(d)
				if validId.Created and validId.AssetTypeId == 3 then
					if ifAllowed(d) then
						print("yes")
					else
						print("no")
					end
				end			
			end
		end
	end)
end)

1 comment

0

Did you check both values in the table because from what I see the problem is most likely that you have the for loop in a way that it can only return true if the very first value in the table is the id

2 answers

You try to check if a string is equal to a number.

Put the sound ID's in the table (line1) as strings and it COULD work.

If not, comment below and i'll have a more brief look.

1 comment

0

I used tostring() but that still didn't work.

Your ifAllowed function has a logic error. You are saying "If, for any entry in the blacklist, the current ID is not equal to the current entry, then you are allowed in." This will always return true so long as you have 2+ entries in the blacklist. You need to instead say "If, the current ID is not equal to any of the entries, then it is allowed." However, if you have numerous IDs, this gets inefficient. Instead, use a dictionary and lookup will be much faster. There are two ways to initialize this dictionary.

Inline:

local blacklist = {[242230929]=true, [320742188]=true}

Conversion loop:

local blacklistIDs = {242230929, 320742188}
local blacklist = {}
for i = 1, #blacklist do
	blacklist[blacklistIDs[i]] = true
end

In either case, after it's a dictionary you can do a lookup like this:

blacklist[id] ex.

function ifAllowed(id)
	return not blacklist[id]
end

If you need to add further entries to blacklist, the syntax is this:

blacklist[newID] = true

If you wanted to do it manually for id 123, it would be:

blacklist[123] = true

Naturally, you can wrap that in a function call if you prefer:

function AddToBlacklist(id)
	blacklist[id] = true
end

2 comments

0

I insert the id's into blacklist using table.insert. (they come from trello). How could I do the conversion with table.insert?

0

I edited the answer for how to add new ids to the dictionary. Either use table.insert before converting, or use the explanation at the bottom of the post to add them after the conversion (and do not use table.insert after the conversion).

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