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

How to make a touch part to clone a tool to your backpack?

How would you set up a part that you touch and it clones a tool to your backpack? I have a script that clones it to my backpack but it has errors and I don't know how to set it up. I would greatly appreciate it if someone could help with this!


local Players = game:GetService("Players") local lantern = game.ReplicatedFirst.Lantern

local part = script.Parent local function onTouched(part)
local player = Players:GetPlayerFromCharacter(part.Parent) if not player then return end local backpack = player.Backpack lantern:Clone().Parent = backpack end part.Touched:Connect(onTouched)


2 comments

0

what are the errors or it just not working?

0

Ah. You defined part twice.

2 answers

✓ Accepted answer

Hi Synavant! In your script, you defined part as script.Parent, but you also defined it as the part that touched the brick.

This should work:

local lantern = game:GetService("ReplicatedFirst"):WaitForChild("Lantern")

local part = script.Parent
function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		lantern:Clone().Parent = player.Backpack
	end
end

If this helped, don't forget to accept my answer!

1 comment

0

How would I set it up to do it though? Like, do I put the script in the part I want cloned or what? Sorry, I am new to scripting and I learn easier if I am told how to do the stuff.

i actually don't know what is the problem, but maybe this will fix it

local Players = game:GetService("Players")
local lantern = game.ReplicatedFirst.Lantern

local part = script.Parent
local function onTouched(Part)  
	if Game.Player:GetPlayerFromCharacter(Part.Parent) then
		local backpack = player.Backpack
		local ClonedLantern = lantern:Clone()
		ClonedLantern.Parent = backpack
	end
end

part.Touched:Connect(onTouched)

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