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

Making tool enter another players backpack due to touching?

Asked by
Avi_i 2
5 years ago
Edited 5 years ago

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)
0
Yeah. You're not using the full update, I made the final tweek, apply that instead Ziffixture 6913 — 5y
0
sent Ziffixture 6913 — 5y

2 answers

Log in to vote
2
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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)
0
the touched event should also be fired before checking the userInput Ziffixture 6913 — 5y
0
I did that and got "newplayer is not a valid member of Players" Avi_i 2 — 5y
0
Switch the input to inputObject:IsKeyDown(Enum.KeyCode.X) this will allow the swap to work Ziffixture 6913 — 5y
0
Because that,s not the right index, this whole script is bad, let me fix it up Ziffixture 6913 — 5y
View all comments (28 more)
0
alright I'll keep checking back to look for it, thanks for sticking with me and helping. Avi_i 2 — 5y
0
iYz, check my answer below it may help! joritochip 705 — 5y
0
You're good to go Ziffixture 6913 — 5y
0
toolHandle's index may be wrong, so just fix that up if you need to Ziffixture 6913 — 5y
0
"attempt to index local 'toolHandle' (a nil value)" Avi_i 2 — 5y
0
You know the index hierarchy, just index the tools handle in toolHandle Ziffixture 6913 — 5y
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 joritochip 705 — 5y
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... Ziffixture 6913 — 5y
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 Ziffixture 6913 — 5y
0
lock toolHandle = workspace:WaitForChild("toolName").Handle, there ya go Ziffixture 6913 — 5y
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 Ziffixture 6913 — 5y
0
nah im fine with it being held down, i'm getting newplayer is not a valid member of Players again as the error. Avi_i 2 — 5y
0
I updated the answer, put it in now Ziffixture 6913 — 5y
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 Avi_i 2 — 5y
0
Use the verson I updated Ziffixture 6913 — 5y
0
Yz, check my answer again! I updated it joritochip 705 — 5y
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 Ziffixture 6913 — 5y
0
Getting "Player is not a valid member of Players" Avi_i 2 — 5y
0
write this Ziffixture 6913 — 5y
0
Actually, I fixed the issue, do what I did in the edit Ziffixture 6913 — 5y
0
Guarantee no more issues from here, if so I'll still stick with ya Ziffixture 6913 — 5y
0
if it works out, don't forget to hit accept answer and upvote! Ziffixture 6913 — 5y
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 Avi_i 2 — 5y
0
Did you apply the update I made? Ziffixture 6913 — 5y
0
Paste the Script you're using above on your question, let me read it Ziffixture 6913 — 5y
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. Avi_i 2 — 5y
0
okay Ziffixture 6913 — 5y
0
That's the final fix, you're good to go Ziffixture 6913 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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!

0
makes the tool disappear from my hands but doesn't enter the person's hands i tried giving the tool to Avi_i 2 — 5y
0
Try it now joritochip 705 — 5y
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. Avi_i 2 — 5y

Answer this question