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

How to clone an object into a players hand when they click a button?

Hello

I have been trying to make a GUI button that will spawn tools into the players hand when clicked. However, i don't know how to find the player that clicked the button (so that the object can be cloned into their inventory). This is what i have done so far.

local coffee = game.ReplicatedStorage.Food.Coffee
local button = script.Parent

button.MouseButton1Down:Connect(function()
	
end)

Thanks!

2 answers

✓ Accepted answer

What you want to do like what @notoriousNoah10 said is create a RemoteEvent into ReplicatedStorage. Then rename it to something like "EquipItem"

Then in a LocalScript inside of your button do:

local coffee = game.ReplicatedStorage.Food.Coffee
local button = script.Parent

button.MouseButton1Down:Connect(function()
	game.ReplicatedStorage.EquipItem:FireServer(coffee)
end)

And now in a server script do:

game.ReplicatedStorage.EquipItem.OnServerEvent:Connect(function(player, item)
	local clone = item:Clone()
	clone.Parent = player.Backpack
	player.Character:WaitForChild("Humanoid"):EquipTool(clone)
end)

Edit: Fixed a typo in the code Tested it and it works.

1 comment

0

Thank you very much!

You're better off using a RemoteEvent for cloning the tool as doing it from a LocalScript will result in replication issues such as the tool not working properly

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