List does not working

Hi, i have make a list of a boss in my server, when a player attack a boss i want to check if the specific boss is in the list, i have make this:

local list = {"Fox", "Yeti", "Crocodile", "Pharaoh"}

When the player attack the boss i have this condition:

if list[HitParent] then
	print("Rangeboss")

HitParent = the name of the boss when the player attack, so for exemple HitParent = Fox

But the print is’nt fire ^^ maybe my condition is not exactly, somebody have a solution for check the name of the boss in the list ?

You index for values in an array with integer indices starting at 1. You are attempting to index with an instance. You can use a dictionary instead.

If you are using table[index], then your table should be like this:

local list = {
"Block1" = "Like",
"Block2" = "This"
}

Instead, you can use table.find(HitParent) to find the HitParent’s name from the type of list you made!

Hope this helps. :smiley:

Thank you for your help, i have made this so:

local list = {

Block1 = "Fox",

Block2 = "Yeti",

Block3 = "Crocodile",

Block4 = "Pharaoh"

}

and

if table.find(list, HitParent) then
	print("Rangeboss")

But it’s not work maybe i have read an incorrect condition :thinking:

You don’t have to add the Block1, Block2 and that if you are using table.find(). You have to use that only if you are indexing.

So, with the current code of yours, the list would be:

local list = {
    "Fox",
    "Yeti",
    "Crocodile",
    "Pharaoh"
}

Hope this helped! :smiley:

The print isn’t fire :confused:

if table.find(list, HitParent) then

This code is supposed to work? :thinking:

If you have an array, you’ll either be using table.find or manually iterating through the table with a needle-in-the-haystack function:

local list = {"Fox", "Yeti", "Crocodile", "Pharaoh"}

local function isInArray(array, _find)
    for _, value in ipairs(array) do
        if value == _find then
            return true
        end
    end
    return false
end

if isInArray(list, HitParent) then
    print("Rangeboss")

If it’s a dictionary, then the original if statement would work, but you just need to format it into a dictionary. You got it at some point in the responses but used table.find which should be for arrays and inverted the dictionary order.

local list = {
    ["Fox"] = "Block1",
    ["Yeti"] = "Block2",
    ["Crocodile"] = "Block3",
    ["Pharaoh"] = "Block4",
}

if list[HitParent] then
    print("Rangeboss")