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?
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
For some reason, it still doesn’t work for me. There are no errors, but here’s a screenshot of the buttons:
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)
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?