Give tools script saying player is nil

Made a super basic script that’s supposed to parent a tool from a folder called “Inventory” to your actual backpack. Problem is it is erroring for some reason

Workspace.GiveTools.GiveTools:8: attempt to index nil with 'WaitForChild' 
local deb = false
wait(3)
script.Parent.Touched:Connect(function(hit)
	if hit then
		if hit.Parent:FindFirstChild("Humanoid") and deb == false then
			deb = true
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			for i,v in pairs(player:WaitForChild("Inventory"):GetChildren()) do
				if v:IsA("Tool") then
					v.Parent = player.Backpack
				end
			end
            deb = false
		end
	end
end)
1 Like

You need to consider if the hit.Parent, is a character that belongs to a player ingame.

if player then
– code
end

2 Likes

That makes more sense. I tried to add wait(3) but this is more efficient. Thanks!

Final script

local deb = false

script.Parent.Touched:Connect(function(hit)
	if hit then
		if hit.Parent:FindFirstChild("Humanoid") and deb == false then
			deb = true
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				for i,v in pairs(player:WaitForChild("Inventory"):GetChildren()) do
					if v:IsA("Tool") then
						v.Parent = player.Backpack
					end
				end
			end
           deb = false
		end
	end
end)
2 Likes