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

Dictionary Searching comes up with no results?

Asked by 3 years ago
Edited 3 years ago

I'm attempting to make a tool giver script, with the name of user's and their tools. If anyone could point me in the right direction, or even solve it yourself, that would be great!

--// added dictionary for addition of more people

local Dictionary = {
    ["IomharOBriain"] = "Poseidon's Touch",
    ["BonesMcClyde"] = "Champion's Trident",
    ["JamesMcClyde"] = "XelGuard"
}

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        for i, v in pairs(Dictionary) do
            if player.Name == Dictionary[i[1]] then
                print("success", i, player.Name)
            else
                print("fail", i, player.Name)
            end
        end
    end)
end)

Output: https://gyazo.com/af6353b57ff3b119d56f8d1ee99553b2

0
You want to get the first argument in the table only? Or find the name in the whole table? NotTheChara 191 — 3y
3
i dont see why you would need to do Dictionary[i[1]]. you can just do i instead. try it Zeuxulaz 148 — 3y
0
Correct, NotTheChara, I want to get the first argument only. And Zeuxulaz, I already tried it with i, and no change. loowa_yawn 383 — 3y
0
I think you should check each arguments to give the tool to the player? Although you can get the first argument only, it can only give the tool to IomharObriain. NotTheChara 191 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

I don't really understand why you must get the first key in the dictionary, but I think this is what you wanted.

--// added dictionary for addition of more people

local toolsToGive = game:GetService("ServerStorage").ToolsToGive

local Dictionary = {
    ["IomharOBriain"] = "Poseidon's Touch",
    ["BonesMcClyde"] = "Champion's Trident",
    ["JamesMcClyde"] = "XelGuard"
}

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        for i, v in pairs(Dictionary) do
            if player.Name == i then   --We check all the keys and see if it is the same as the player's name
                for index, tool in pairs(toolsToGive:GetChildren()) do   --If it is, get the tools in a Folder named ToolsToGive within ServerStorage
                    if tool.Name == v then   --If the tool's name is the same as the value name
                        print("success", i, player.Name)
                        tool:Clone().Parent = player:WaitForChild("Backpack")   --We give the tool to that player.
                    end
                end
            else
                print("fail", i, player.Name)
            end
        end
    end)
end)

Explanation is inside the code block.

0
Why is there extra contents in the script included, although, it wasn't mentioned in the question? BestCreativeBoy 1395 — 3y
0
He mentioned that he is trying to make a tool giver script. NotTheChara 191 — 3y
0
Well, I would recommend to clarify the doubts of the asker and help him and not feed. BestCreativeBoy 1395 — 3y
0
Ehm, I explained how can he do better and why should he do this. NotTheChara 191 — 3y
0
Cool, this is exactly what I was looking for! Many thanks. loowa_yawn 383 — 3y
Ad
Log in to vote
0
Answered by 1 year ago

hahha, looking back on this two years later, sadly cringing at this sight of mess.

--// added dictionary for addition of more people

local SwordKeeper = {
    --[[
    ["IomharOBriain"] = "Poseidon's Touch",
    ["BonesMcClyde"] = "Champion's Trident",
    ["JamesMcClyde"] = "XelGuard"

    replace usernames with userids 
    ]]
    [110029109] = "Poseidon's Touch"
}

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        local PlayerSword = SwordKeeper[player.UserId]
        if not PlayerSword then
            -- player does not have a custom sword that needed to be given
            return
        end
        -- .. give player their custom sword
    end)
end)

Answer this question