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