Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how do i find a string in a table?

Asked by 2 years ago

Hey. I want to search a table for a string. I already tried table.find(table, string), but it always spits out nil.

heres the table:

local emojis = {
    --Standard emoji set
    ["standard"] = {

        ["common"] = {

            ["Grinning_Face"] = {
                Name = "Grinning face",
                Emoji = "????",
                Rarity = "Common",
                Cost = 50
            },

            ["Beaming_Face"] = {
                Name = "Beaming face",
                Emoji = "????",
                Rarity = "Common",
                Cost = 50
            },

            ["Grinning_Squinting_Face"] = {
                Name = "Grinning squinting face",
                Emoji = "????",
                Rarity = "Common",
                Cost = 60
            },

            ["Smiling_Face"] = {
                Name = "Smiling face",
                Emoji = "????",
                Rarity = "Common",
                Cost = 40
            },

            ["Laughing_Face"] = {
                Name = "Laughing face",
                Emoji = "????",
                Rarity = "Common",
                Cost = 70
            },

            ["Winking_Face"] = {
                Name = "Winking face",
                Emoji = "????",
                Rarity = "Common",
                Cost = 70
            },

            ["Cheeky_Face"] = {
                Name = "Cheeky face",
                Emoji = "????",
                Rarity = "Common",
                Cost = 70
            }

        },

        ["uncommon"] = {

            ["Angel_Face"] = {
                Name = "Angel face",
                Emoji = "????",
                Rarity = "Uncommon",
                Cost = 180
            },

            ["Goofy_Face"] = {
                Name = "Goofy face",
                Emoji = "????",
                Rarity = "Uncommon",
                Cost = 170
            },

            ["Star_eyed_face"] = {
                Name = "Star eyed face",
                Emoji = "????",
                Rarity = "Uncommon",
                Cost = 150
            }

        },

        ["rare"] = {

        },

        ["very_rare"] = {

        },

        ["extreamly_rare"] = {

        },

        ["mega_rare"] = {

        }

    }

}

and this is my code:

function emojis.GiveEmoji(PlayerName, Emoji)
    print(table.find(emojis, Emoji))
    if table.find(emojis, Emoji) == nil then
        print("invalid emoji name")
    else
        local invGui = game.Players[PlayerName].PlayerGui:WaitForChild("Inventory")
        if invGui.Frame.ScrollingFrame:FindFirstChild(Emoji) then
            local Em = invGui.Frame.ScrollingFrame[Emoji]

            Em.Count.Value = Em.Count.Value + 1
            Em.TextLabel.Text = Emoji.." x"..Em.Count.Value
        else
            local clone = invGui.InvContent.ImageButton:Clone()
            clone.Name = Emoji
            clone.TextLabel.Text = Emoji
            clone.Count.Value = 1
            clone.Parent = invGui.Frame.ScrollingFrame
            clone.Visible = true
        end
        print("gave emoji ".. Emoji.. " to ".. PlayerName)
    end
end

and if you are wondering why the function name is emojis.GiveEmoji: its because all of this is in a module script. I hope someone can help, thanks in advance!

0
I assume that the parameter "Emoji" is the key of the dictionary. table.find() will find the value in the dictionary and not the key. So you'd need to do this "if not emojis[Emoji] then" instead of "if not table.find(emojis, Emoji) then" MarkedTomato 810 — 2y

Answer this question