Attempt to index nil with 'Parent'

Can someone help me, my code shows this error when I hover over something that is not a model (like the Baseplate)

function deleteselect()
	if dl == true then
		local sl = game.Players.LocalPlayer:GetMouse().Target
		--get the model
		--print(sl)
		if sl then
			model = sl
			--continue for find a folder
			if model.Parent then
				while model.Parent ~= game.Workspace.Builded do
					model = model:FindFirstAncestorOfClass("Model")
					if model then
						if model.Parent.Name == "Builded" then
							break
						end
					end
				end
			end
1 Like

Since you redefine the model variable to Instance:FindFirstAncestorOfClass("Model"), this method will return nil for any Instance that doesn’t a Model as an ancestor, and thus, the model variable will also become nil.

To add to what @HugeCoolboy2007 said, why not just use something like

if not model:FindFirstAncestorOfClass("Model") then return end
model = model:FindFirstAncestorOfClass("Model")

(Also why “Builded” instead of “Built”?)

Thank you, it worked!

That’s because I’m still learning English :slight_smile: My native language is Portuguese

1 Like

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