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)
~~~~~~~~~~~~~~~~~
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!
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)