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

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

Asked by 2 years ago

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

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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.

0
Thank you very much! MOLFLINT 6 — 2y
Ad
Log in to vote
1
Answered by 2 years ago

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

Answer this question