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

How do I clone a tool in the workspace and put it in a players back pack when a part is touched?

I am trying to create a script within a part that when the part is touched, will clone a tool from within the workspace and put it in the players backpack. Any help is appreciated, thanks in advance!

Here is the script I have came up with that is not working:

local tool = game.Workspace.FryingPan
local player = game.Players.LocalPlayer --finds the script's parent, the player.

function Touch(hit)--when clicked, function

	tool:Clone() --clones tool
	tool:Clone().Parent = player.Backpack --has the cloned tool's parent the player's Backpack
end

script.Parent.Touched:Connect(Touch)

1 answer

if i'm correct, you're doing this in a ServerScript and using Player.LocalPlayer will not work. instead, you're gonna use player.PlayerAdded or :GetPlayerFromCharacter

local tool = game.Workspace:WaitForChild("FryingPan")
local player = game:GetService("Players")

function touched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- If it's a humanoid
		local plr = player:GetPlayerFromCharacter(hit.Parent) -- we get the player from the character
		if plr then -- if player exist then
			tool:Clone().Parent = plr.Backpack
		end
	end
end

script.Parent.Touched:Connect(touched)

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