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

How do you clone a tool into a players inventory from a touched event?

Asked by 4 years ago

Hi, I'm trying to make a part that when you step on it it clones a tool into the person who stepped on it's backpack. I did this code but it said attempt to index nil with Backpack.

script.Parent.Touched:Connect(function(player)

    local humanoid = player.Parent:FindFirstChild("Humanoid")

    if humanoid ~= 0 then

        local backpack = game.Players:FindFirstChild(player.Name):FindFirstChild("Backpack")

        local clonedTool = script.Parent.Burger:Clone()

        clonedTool.Parent = backpack

    end

end)

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

There are two issues to point out within your program. The parameter that is supplied containing the Instance on the opposing contact, is merely a body part if theoretically being touched by a Character Rig. You attempt to search the Players container for this Object which won't return a proper result. Lastly, you're trying to compare a number to the Object Humanoid, this won't compare properly, so the conditional won't run the code below. I assume you're trying to check their Health, that of which is a float, so we can evaluate:


Note:

We can use the :GetPlayerFromCharacter() method of Players to uplink a Player Object with the Character derived from Hit.Parent.


local Players = game:GetService("Players")

local Tool = script.Parent

local Burger = Tool.Burger

Tool.Touched:Connect(function(Hit)
    local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
    if (Humanoid and Humanoid.Health > 0) then
        local Player = Players:GetPlayerFromCharacter(Hit.Parent)
        local Backpack = Player:WaitForChild("Backpack")
        Burger:Clone().Parent = Backpack
    end
end)

It's best to maintain a practice of filtering for a Humanoid within a .Touched signal to ensure the chances of Hit.Parent correlating to a Character.

If this helps, don't forget to accept this answer!

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hello, Krystal_Kyle. The problem with your script is that you wrote player.Name instead of player.Parent.Name. Also, I'd call the parameter "hit" instead of "player" as it can sometimes confuse you. Try this:

script.Parent.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

    if player then
        local clonedTool = script.Parent.Burger:Clone()
        clonedTool.Parent = player.Backpack
    end
end)

The variable called player will get the player from its character which is hit.Parent. Then there's an if statement checking if the part hit is a player. If it is, the clonedTool's parent will get set to the player's backpack. Also, I used :GetService() as it checks if the service is existent. If it isn't, it creates the service. Please upvote and accept this answer if it helped.

Answer this question