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

Dictionary Searching comes up with no results?

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

4 comments

0

You want to get the first argument in the table only? Or find the name in the whole table?

3

i dont see why you would need to do Dictionary[i[1]]. you can just do i instead. try it

0

Correct, NotTheChara, I want to get the first argument only. And Zeuxulaz, I already tried it with i, and no change.

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.

2 answers

✓ Accepted answer

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.

5 comments

0

Why is there extra contents in the script included, although, it wasn't mentioned in the question?

0

He mentioned that he is trying to make a tool giver script.

0

Well, I would recommend to clarify the doubts of the asker and help him and not feed.

0

Ehm, I explained how can he do better and why should he do this.

0

Cool, this is exactly what I was looking for! Many thanks.

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)

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