How to make a button give the player a tool that shares the text of a text label

Yeah, I feel really stupid right now.

I have a folder in ReplicatedStorage called “ToolsFolder” and have 3 tools in it. Let’s say I want to retrieve the second tool named “Tool” and I click a text button that has the text “Tool”. How would I make it so that it clones the tool to the player backpack?

Upon the button being clicked use :FindFirstChild(idk script.Parent.Text) where the Tool is being located

EDIT: Make sure to remotes, look up how to use them also.

Reference replicated storage on a localscript parented to the text button:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

Then create a mousebutton event to connect it to the button:

script.Parent.MouseButton1Down:Connect(function()
end)

Inside the function, search replicated storage for the tool based on what text is currently in the button. Check if it is found first to avoid an error if the tool doesn’t exist. If it is found, clone it, then parent it to the LocalPlayers’ backpack:

local tool = ReplicatedStorage.ToolsFolder:FindFirstChild(script.Parent.Text)
if tool then
	local copy = tool:Clone()
	copy.Parent = game:GetService("Players").LocalPlayer.Backpack
end

Full code would look something like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Down:Connect(function()
	local tool = ReplicatedStorage.ToolsFolder:FindFirstChild(script.Parent.Text)
	if tool then
		local copy = tool:Clone()
		copy.Parent = game:GetService("Players").LocalPlayer.Backpack
	end
end)

As @NDavis06 mentioned it would be wise to keep these tools hidden from the player (such as in ServerStorage) and use a remote event connection to allow the server to handle cloning tools not the player. Naughty players can grab anything they want from ReplicatedStorage.
*Edit to reflect the “ToolsFolder” in OP

1 Like

For some reason, it still doesn’t work for me. There are no errors, but here’s a screenshot of the buttons:
image
Might be because I put the ImageButton inside of the TextLabel. I edited the script like so:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Down:Connect(function()
	local tool = ReplicatedStorage.ToolsFolder:FindFirstChild(script.Parent.Parent.Text)
	if tool then
		local copy = tool:Clone()
		copy.Parent = game:GetService("Players").LocalPlayer.Backpack
	end
end)

Let’s try making some debug statements to see where it is failing:

script.Parent.MouseButton1Down:Connect(function()
	print("Button pressed, trying to find a tool " .. script.Parent.Parent.Text)
	local tool = ReplicatedStorage.ToolFolder:FindFirstChild(script.Parent.Parent.Text)
	
	if tool then
		print("Found tool, parenting..")
		local copy = tool:Clone()
		copy.Parent = game:GetService("Players").LocalPlayer.Backpack
	else
		print("Tool does not exist. Possible results are:")
		for k, v in pairs(ReplicatedStorage.ToolFolder:GetChildren()) do
			print(v.Name)
		end
	end
end)

Yeah, it is trying to find the tool.

But no further message if it’s found or not? I made a quick replication in studio and it works for me. It looks like the “Three” is padded on the left with spaces?

Oh. It gave me an error message.

image
Here’s a screenshot of the ReplicatedStorage folder.

My mistake. I wrote “ToolFolder” instead of “ToolsFolder” does changing that fix it?

1 Like

well yes, because that seems to be your error message.

1 Like