Attempt to index nil with Item

(Yes, believe me, I searched and nothing worked)
I have a script that works in studio but not in the real game. Here’s the script:

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local ToolCount = 0
	for i, v in pairs(player.Character:GetChildren()) do
		if v.ClassName == "Tool" then
			ToolCount += 1
		end
	end
	for i, v in pairs(player.Backpack:GetChildren()) do
		ToolCount += 1
	end
	if ToolCount < player.Character.InventorySlots.Value then
		local Item = workspace["Flash Light"]:Clone()
		script.Parent:Destroy()
		Item.Parent = player.Backpack
	else
		if player.PlayerGui.ScreenGui.TooMany.TextTransparency == 0 then
			local count = 0
			player.PlayerGui.ScreenGui.TooMany.Visible = true
			repeat
				player.PlayerGui.ScreenGui.TooMany.TextTransparency += 0.01
				count += 1
				wait(0)
			until count == 100
			player.PlayerGui.ScreenGui.TooMany.Visible = false
			player.PlayerGui.ScreenGui.TooMany.TextTransparency = 0
		end
	end
end)

The error is: Workspace.Crate.ProximityPrompt.Flash Light.Script:14: attempt to index nil with ‘Item’. Why does this happen?

You receive this error when you try and get a property that is nil, in this case the property you are trying to get is from “Item”.

Maybe try script.Parent:Destroy() after you set the items parent?

It works now, I don’t know why that works, but it does. Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.