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?

01local tool = game.ServerStorage.TestTool --Change TestTool to whatever you want
02local inventory = tool:Clone()
03 
04function onTouched(hit)
05    local player = game.Players:GetPlayerFromCharacter()(hit.Parent)
06    if player:FindFirstChild("Backpack") then
07        inventory.Parent = player.Backpack
08    end
09end
10script.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

01-- Also always put gears that need to be cloned in replicatedStorage as that is the storage ment for cloning not serverstorage
02 
03 
04function onTouched(hit)
05    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- u did brackets wrong
06    if player then -- just check if its a player no need to check the backpack
07local tool = game.ReplicatedStorage.TestTool:Clone() -- clone it here or else itll only clone once globally and only 1 person can get the tool
08        tool.Parent = player:WaitForChild("Backpack") -- use waitforchild here
09    end
10end
11script.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