Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Making tool enter another players backpack due to touching?

I'm trying to make it so when a player presses x and the tool touches a player, the tool goes into the player's inventory who got touched with the tool.

local UserInputService = game:GetService("UserInputService")
	local ToolHandle = script.Parent.Handle
	ToolHandle.Touched:Connect(function(Part)
	   if (UserInputService:IsKeyDown(Enum.KeyCode.X)) then
	      local Humanoid = Part.Parent:FindFirstChild("Humanoid")
	      if (Humanoid) then
	         local Player = tostring(Humanoid.Parent.Name); print(Player)
	         script.Parent = game.Players.Player:WaitForChild("Backpack")
	     end
	  end
end)

2 comments

0

Yeah. You're not using the full update, I made the final tweek, apply that instead

0

sent

2 answers

✓ Accepted answer

Your Script is quite messed up, let me fix it up for you, this also includes fixes so follow what I've done:

local UserInputService = game:GetService("UserInputService")
local Tool = workspace:WaitForChild("toolName")
local ToolHandle = Tool:FindFirstChild("Handle")
ToolHandle.Touched:Connect(function(Part)
   if (UserInputService:IsKeyDown(Enum.KeyCode.X)) then
      local Humanoid = Part.Parent:FindFirstChild("Humanoid") or Part.Parent,Parent:FindFirstChild("Humanoid")
      if (Humanoid) then
         local Character = Humanoid.Parent; local Player = game.Players:GetPlayerFromCharacter(Character)
         Tool.Parent = game.Players[Player.Name]:WaitForChild("Backpack")
      end
   end
end)

32 comments

0

the touched event should also be fired before checking the userInput

0

I did that and got "newplayer is not a valid member of Players"

0

Switch the input to inputObject:IsKeyDown(Enum.KeyCode.X) this will allow the swap to work

0

Because that,s not the right index, this whole script is bad, let me fix it up

0

alright I'll keep checking back to look for it, thanks for sticking with me and helping.

0

iYz, check my answer below it may help!

0

You're good to go

0

toolHandle's index may be wrong, so just fix that up if you need to

0

"attempt to index local 'toolHandle' (a nil value)"

0

You know the index hierarchy, just index the tools handle in toolHandle

0

I don't think he wants it to only go off if X is being held down, but instead only have X pressed once then it is "activated" like a hot potato

0

This Script is running primarily based of of the structure of a Tool, the Handle's parent must be a Tool, and the Tool must be in workspace, try this...

0

If he's holding down x as he touches the Part, then it goes into his backpack, if he presses it after he touched it wouldn't fire, this method is a lot more efficient for this scenario

0

lock toolHandle = workspace:WaitForChild("toolName").Handle, there ya go

0

I've applied crucial upadtes to the answer, just replac "toolName" with the Tool's name, and make Sure the tool is in workspace, and the Handle is the first Child of Tool

0

nah im fine with it being held down, i'm getting newplayer is not a valid member of Players again as the error.

0

I updated the answer, put it in now

0

I have edited the code above so u can see what I have incase i'm doing anything wrong, I changed toolHandle so it would actually call for the Handle but thats about it

0

Use the verson I updated

0

Yz, check my answer again! I updated it

0

Please make sure you're index through the index method I provided, also that the Tool is in workspace, write the tools name where "toolName" is

0

Getting "Player is not a valid member of Players"

0

write this

0

Actually, I fixed the issue, do what I did in the edit

0

Guarantee no more issues from here, if so I'll still stick with ya

0

if it works out, don't forget to hit accept answer and upvote!

0

it prints "Binary_i" who is the person who i'm trying to give the tool to, but then it says "Player is not a valid member of Players" sort of odd

0

Did you apply the update I made?

0

Paste the Script you're using above on your question, let me read it

0

ok I did just that I removed the Tool variable because that called for an infinite yield and the tool isnt in workspace. If u can add me on Discord @Avi#3700 so i can show screenshots and stuff so maybe that'll be more helpful and cut time spent on this script down.

0

okay

0

That's the final fix, you're good to go

Try this code instead:

game:GetService("UserInputService").InputBegan:connect(function(Input, GPE)
    if script.Parent.Parent.ClassName ~= "Model" then return end
    if script.Parent.Parent:FindFirstChild("Humanoid") == nil then return end -- Ensures tool is equipped

    if Input.KeyCode == Enum.KeyCode.X and not GPE then
        script.Parent.Handle.Touched:Connect(function(Hit)
            if Hit.Parent == script.Parent.Parent then return end -- Don't give it to yourself!

            local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
            if Player then
                if Hit.Parent:FindFirstChild("Humanoid") ~= nil then
                    Hit.Parent.Humanoid:EquipTool(script.Parent)
                end
            end
        end)
    end
end)

Did this help? Mark it as correct and upvote! Thanks!

3 comments

0

makes the tool disappear from my hands but doesn't enter the person's hands i tried giving the tool to

0

Try it now

0

Doesn't work, doesn't show any error though, my guess would be because the tool is in the right hand (which would mean touching my player) so it just returns end.

This discussion is preserved as it was. New answers, comments, and votes are closed.