Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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) 

4 comments

0

Put the hit.Parent inside of the GetPlayerFromCharacter brackets and add a check to see if whatever touched it has a humanoid 

0

How? Also I fixed that the brackets thing.

0

u have to use :WaitForChild() instead of :FindFirstChild() for some reason

0

Now I'm getting attempt to index nil with WaitForChild() What kind of garbage is this coding?

1 answer

✓ Accepted answer

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

1 comment

0

Thanks, it's working

This discussion is preserved as it was. New answers, comments, and votes are closed.