Attempt to index nil with Clone

This is a support category for asking questions about how to get something done on the Roblox websites or how to do something on Roblox applications such as Roblox Studio.

You can write your topic however you want, but you need to answer these questions:
1.

Im making a project for fun and it involves stages being randomly generated every time. Some stages include objects that run on every players individual client.

When using a remote event to try to clone the client objects to the client, it says- " attempt to index nil with ‘Clone’"

I tried to print debug and the object used to identify the client sided object is there but when printing the parent it returns nil. The parent looks like it exists

Here is the script for the stage generator i made:

Server Script:

local function addFloor(floor)
	local randomStage = nil
	if floor == nil then
		randomStage = math.random(1, #storedstages)
	end
	if floor ~= nil then
		randomStage = floor
	end

	local clonedStage = storedstages[randomStage]:Clone()

	clonedStage.Parent = game.Workspace.Stages
	
	table.insert(Stages,clonedStage)
	
	local posi= Vector3.new(Base.PrimaryPart.Position.X,(Base.PrimaryPart.Position.Y+height),Base.PrimaryPart.Position.Z)
	
	
	local rotfactor = #Stages+1
	local rotation = (game.Workspace.Base.PrimaryPart.Orientation.Y)+(180*rotfactor)
	clonedStage:SetPrimaryPartCFrame(CFrame.new(posi) * CFrame.fromEulerAnglesXYZ(0, math.rad(rotation), 0))
	--clonedStage:SetPrimaryPartCFrame()
	--clonedStage:MoveTo(Base.PrimaryPart.Position)
	local newHeight = clonedStage:FindFirstChild("Walls"):GetExtentsSize().Y+height
	height =  newHeight
	for i,v in pairs(clonedStage:GetDescendants()) do
		game.ReplicatedStorage.RestartCos:FireAllClients(v)
	end
end

local function createTower(floorcount)
	height = Base:GetExtentsSize().Y
	print(Stages)
	--clear old stages
	for i,v in pairs(Stages)do
		v:Destroy()
		Stages[i]=nil
	end
	print(Stages)
	local count = nil
	if floorcount == nil then
		count = math.random(5,6)
	else
		count = floorcount
	end
	for i = 1,count,1 do
		addFloor()
		print(Stages[#Stages]:GetDescendants())
	end
end
local function startTimer()
	print("starting timer")
	local startTime = game:GetService("ServerStorage"):WaitForChild("GameSettings"):FindFirstChild("Timer").Value
	for i = startTime,0,-1 do
		currentTime.Value = i
		wait(1)
	end
end


local function newRound()
	createTower()
	--startTimer()
	for i,v in pairs(workspace.Stages:GetDescendants()) do
		game:GetService("ReplicatedStorage").RestartCos:FireAllClients(v)
		print(v.Parent)
		
	end
	print("g")
	if currentTime.Value == -1 then
		newRound()
	end
end

Local Script (this script portion originates from an obstacle course kit):


local addPart = function(v)
	if v.Name == "ClientObject" then
		print(v)
		local c = v.Parent:Clone()
		c.Parent = v.Parent.Parent
		v.Parent:Destroy()
		addChildren(v,c)
		ApplyPart(c)
		for j,w in pairs(c:GetDescendants()) do
			ApplyPart(w)
		end
	end
end

This portion of the script attempts to clone any of the objects to the client.
The function ApplyPart() requires a module script inside of the parent of the part used to identify the client sided object

Is Archivable being set to false anywhere? Characters by default are Archivable off.

Wait a sec… “attempt to index nil with ‘Clone’?” You’re attempting to clone a nil object! Use WaitForChild or some if checks to make sure that what you’re cloning exists.

EDIT: Try adding a string to stored stage names and concatenate in code when randomly generated. So instead of workspace having 1, 2, 3, it has Stage1, Stage2, Stage3. Then in code to access random stages write randomStage = "Stage"..math.random(1, #storedstages)

The objects are in serverstorage and cloned to the server using a server script. The client should be checking to find the clientobject objects to identify them but when i try to get the parent of those parts, it returns them as nil. I checked to see if the archivable property is false but it isn’t.

the main issue isnt the stages being cloned to the workspace, its the clientobjects being cloned to the client. i apologize for not being a little clearer in the original question

The client CANNOT read ServerStorage. If you need something on the client, use ReplicatedStorage. Also, objects that are :Destroy()ed or :Remove()ed have a Parent of nil, as far as I know.

EDIT: What does addChildren do? Is this custom code or built-in?

I know that but it doesnt work AFTER i clone it to the workspace though

addChildren tries to make the children of the objects being cloned to the client also clone to the client. The only part that doesnt work is the addPart function

It errors when I try to call it on a part that is created after the script is ran.

edit- i also tried the same thing on replicated storage but it still gets the parent of an object as nil after its cloned

Maybe the clild gets replicated first, then the parent. Try passing the parent and name of the child, and find the child with waitforchild in the client script.

If remote events are involved, some limitations apply. It doesn’t seem to be the problem, though. (Argument Limitations for Bindables and Remotes | Roblox Creator Documentation)

What does addPart do? Is it important? What purpose does it serve?

i think this might be the issue. Ill try this solution when im able to

Thank you for this it works! I had to get the parent first.

Thank you also for trying to help.I appreciate it a lot

1 Like

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