Ad
Log in to vote
0

If Tool is Touched By a Player then, put in their inventory help?

Asked by 3 years ago

I've been having trouble trying to figure out why my code isn't working, basically- I'm trying to make that if it is touched by a player, it will be put into their inventory.

Code:

tool = script.Parent
Handle = tool.Parent


   enabled = false
    script.Parent.Touched:Connect(function(OnTouched)
    h = script.Parent.Players.PlayerAdded:FindFirstChild("Humanoid")
    if h == true then
    if debounce == false then 
        debounce = true
        tool.Equipped:Connect(function()
            if h == false then
                return
            end
            end)
0
Doesn’t it automatically do that Rynappel 212 — 3y

2 answers

Log in to vote
2
Answered by
pwx 1546 Moderation Voter
3 years ago

Although the tool itself does not have a Touched() event, the handle does. You almost have it just a few adjustments needed.

Tool = script.Parent -- Define your tool.
Handle = Tool:WaitForChild('Handle') -- Define the thing that needs to be touched, in this case your handle.

Players = game:GetService('Players')

Handle.Touched:Connect(function(otherPart)
    local Player = Players:GetPlayerFromCharacter(otherPart.Parent)
    if Player then -- Check if the thing touching it is an actual player.
        Tool.Parent = Player.Backpack -- Parent the tool to their inventory/backpack.
    end
end)
0
^ Also do note, if you want to prevent it glitching inside player's inventory, I suggest you add an if statement detecting if the tool is already picked up or not. pwx 1546 — 3y
0
Thank you so much. Auxatier 59 — 3y
0
Nice, such a great scripter. Xapelize 2637 — 3y
Ad
Log in to vote
0
Answered by
Xapelize 2637 Moderation Voter Community Moderator
3 years ago

There no debounces in the script, so removes the debounce.

Next, some of the if-then and functions if doesn't have an end, so add it.

So basically the script is like this:

tool = script.Parent
Handle = tool.Parent

enabled = false

script.Parent.Touched:Connect(function(OnTouched)
    h = script.Parent.Players.PlayerAdded:FindFirstChild("Humanoid")
    if h == true then
    tool.Equipped:Connect(function()
        if h == false then
        return 
        end
    end)
end
end)

I hope this helps you! Bye bye~~~

0
Do I put the tool in the workspace, and when touched it inserts into the players backpack? Auxatier 59 — 3y
0
Theres an answer so, uh, see that lol Xapelize 2637 — 3y

Answer this question