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

Workspace.Part.OnTouched:6: attempt to index nil with 'FindFirstChild'?

Asked by 4 years ago

Help, I have this error that doesn't mean ANYTHING to me. So I have this part that when it ontouched, it is supposed to clone the tool and put it inside the backpack. It doesn't clone it and it just moves the tool instead. Can anybody tell me what is going on?

local tool = game.ServerStorage.TestTool --Change TestTool to whatever you want
local inventory = tool:Clone()

function onTouched(hit) 
    local player = game.Players:GetPlayerFromCharacter()(hit.Parent)
    if player:FindFirstChild("Backpack") then
        inventory.Parent = player.Backpack
    end
end 
script.Parent.Touched:connect(onTouched) 
0
Put the hit.Parent inside of the GetPlayerFromCharacter brackets and add a check to see if whatever touched it has a humanoid  DarkDanny04 407 — 4y
0
How? Also I fixed that the brackets thing. squidiskool 208 — 4y
0
u have to use :WaitForChild() instead of :FindFirstChild() for some reason TNTIsLyfe 152 — 4y
0
Now I'm getting attempt to index nil with WaitForChild() What kind of garbage is this coding? squidiskool 208 — 4y

1 answer

Log in to vote
1
Answered by
TNTIsLyfe 152
4 years ago
Edited 4 years ago

There are a few bugs with ur script but ill give u the fixed script and explain them

 -- Also always put gears that need to be cloned in replicatedStorage as that is the storage ment for cloning not serverstorage


function onTouched(hit) 
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- u did brackets wrong
    if player then -- just check if its a player no need to check the backpack
local tool = game.ReplicatedStorage.TestTool:Clone() -- clone it here or else itll only clone once globally and only 1 person can get the tool
        tool.Parent = player:WaitForChild("Backpack") -- use waitforchild here
    end
end 
script.Parent.Touched:connect(onTouched) 

Hope this helped u explain all the bugs with ur script

0
Thanks, it's working squidiskool 208 — 4y
Ad

Answer this question