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