Tool not being picked up

Hello everyone I am having trouble I am trying to make a script so that when a player touches a block then it goes to their inventory Tool is already defined and works but its just the finding if the Touched is a player. I made this script because when I created a Tool using local Tool = Instance.new("Tool") and put the block inside it named Handle it didn’t pick it up. This whole script has been me trying to fix it because my main problem was that you couldn’t pick the tool up so please if you can show me how to do it or what’s wrong with my script that will be amazing thanks.

			function Touched(Player)
				local a = game:FindFirstChild("HumanoidRootPart")
				local b = a.Parent
				if Player == b.child then
					local Name = Player.Name
					local inv = game.Players:FindFirstChild(Name)
					Tool.Parent = inv.Backpack
				end
			end

This will find an object named “HumanoidRootPart” in game, not the player’s character if I’m not wrong

do like this:

function Touched(Player)
				local plr = game.Players:GetPlayerFromCharacter(Player.Parent)
				if Player == plr then
					Tool.Parent = plr:WaitForChild("Backpack")
				end
			end

I’m on mobile, so if there’s anything wrong in this, you can fix it.

2 Likes

You’ll need to get the player via the .Touched parameter.
Here’s an example:

local Players = game:GetService('Players')
local Object = Your_Object_Location
local Tool = Your_Tool_Location


local function Touched(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if not Player then return end -- Stop the code from running if player doesn't exist.
	local Backpack = Player:FindFirstChild('Backpack')
	if not Backpack then return end -- Stops the code from running if the backpack doesn't exist.
	Tool:Clone().Parent = Backpack
end


Object.Touched:Connect(Touched)

Oops, I forgot to add that! lol

Explanation: Clones the tool and sets the Backpack as a parent

If there’s no :Clone() then it was a one use now lol

aight I think I’ll sleep then…

Is this a server script or a local script?

just a script in a folder in workspace

try creating the tool without using a script, add the block inside the tool, and put the script inside the tool (you can just insert a tool in workspce).

nope it still doesn’t work it just has the same out come as before

There is also many problems with your script that you’ve shown. You are mentioning HumanoidRootPart in the game, as though HumanoidRootPart is only in a player’s character in Workspace.
Here is a fixed version of your script:

local Tool = script.Parent -- script should be inside the tool as I mentioned above
local Handle = Tool.Block -- Put what your block name is
function onTouched(hit)
   local player = game.Players:GetPlayersFromCharacter(hit.Parent)
   if player then -- If player that touched the part exists
      Tool:Clone().Parent = player.Backpack -- Tool will be cloned and goes into the player's inventory
   end
end

Handle.Touched:Connect(onTouched)

I dont know if you understand but the reason i put humanoid root part was to check if a player touches the part and if they do put the script into their backpack

humanoidrootpart is not the only place where an object can touch, it can touch anywhere on the player’s character.

thats why i did humanoidrootparts parent then child

You were mentioning humanoidrootpart in the game, not in game.Workspace.(specified player).HumanoidRootPart. Try the script that I have given and see if it works

no it doesn’t work it stays as a tool in the workspace

Is there any error in the output?

no their is no error in the output for this script and also i dont want to clone the part i want to move it to the players backpack

Okay. Is the code that you’ve given the only code that is in the script, or is there more?

their is more do you want me to show u? like to say yes