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

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

Asked by 2 years ago

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

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

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)
Ad

Answer this question