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

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

Asked by 4 years ago
Edited 4 years ago

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)

~~~~~~~~~~~~~~~~~

0
what are the errors or it just not working? asdfghjk9019 225 — 4y
0
Ah. You defined part twice. LennyPlayzYT 269 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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!

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. Synavant 6 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)

Answer this question