Attempted to index nil with 'GetDescendants'

i have a function that im trying to test. this is the test function.

local function createGhostModel(model) 
	if not model:IsA("Model") then return end
	print(model)
	local newModel = model:Clone()
	print(newModel)
	
	for i,v in ipairs(newModel:GetDescendants()) do
		if v:IsA("Motor6D") then
			v:Destroy()
		elseif v:IsA("BasePart") then
			v.Anchored = true
		end
	end
end	

for some odd reason, every time i try to use the function with something , an error in the output implies that the newModel variable is nil. even though the character model returns fine. I genuinely don’t understand what is wrong and this is how i got the character model.

player.CharacterAdded:Connect(function(char)
	character = char
	createGhostModel(char)
end)

if anyone can help me find the issue thanks in advance
the function is a LocalScript in StarterPlayerScripts

What do you mean it returns nil? The function createGhostModel isnt supposed to return anything since you didn’t make it return anything.

Assuming what you want is to clone the character though, make sure you set model.Archivable = true before doing model:Clone(), then newModel will be the cloned model.

local function createGhostModel(model) 
	if not model:IsA("Model") then return end
	
	local archivableBefore = model.Archivable
	model.Archivable = true
	
	print(model)
	local newModel = model:Clone()
	print(newModel)
	
	model.Archivable = archivableBefore

	for i,v in ipairs(newModel:GetDescendants()) do
		if v:IsA("Motor6D") then
			v:Destroy()
		elseif v:IsA("BasePart") then
			v.Anchored = true
		end
	end
	
	return newModel
end	
1 Like

i meant that the error implied that newModel doesn’t exist. Sorry for the confusion.

Try using that function I gave that should fix the error you’re getting

the function did work but i don’t really understand why

Characters are set to Model.Archivable = false by default, so you gotta change it to true

https://create.roblox.com/docs/reference/engine/classes/Instance#Archivable

1 Like

I have a somewhat similar problem but with the error
attempt to index nil with 'Destroy' - Client

local function createGhost(part)
	part.Archivable = true
	local new = part:Clone()
	new.CanCollide = false
	new.CanQuery = false
	new.CanTouch = false
	new:FindFirstChildWhichIsA("Motor6D"):Destroy()
	new.Parent = workspace
	new.Anchored = true
end

this is how i used the function

    for i,v in ipairs(character:GetDescendants()) do
		if v:IsA("BasePart") then
			createGhost(v)
		end
	end

Try doing a check to see if there is a motor inside the part. Maybe the motor in the part you’re trying to find in the for loop just isn’t there.

local function createGhost(part)
	part.Archivable = true
	local new = part:Clone()
	new.CanCollide = false
	new.CanQuery = false
	new.CanTouch = false
	
	local part_motor = new:FindFirstChildOfClass("Motor6D")
	if part_motor then
		part_motor:Destroy()
	end

	new.Parent = workspace
	new.Anchored = true
end
2 Likes

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