Instance creating thousands of instances?

Hey developers, I have been attempting to create a custom function which creates an instance when called upon, but when I called the function, It created thousands of the instance:

Script:

local function create(p, n)
	local createdInstance = Instance.new('Folder')
	
	createdInstance.Name = n
	createdInstance.Parent = p
	
	return(create(p, n))
end


create(workspace, 'john')

Any help, tips or suggestions would be greatly appreciated,
Thanks

1 Like

Change it to

return createdInstance

that way it returns the created instance to the caller.

What you did was return the result of create(p, n) from the function create. In other words, the function is calling itself. This is called recursion and is really powerful, but the way you’re doing it, every call the create has to evaluate another call to create before it can return, and those calls must also call create, and so forth to infinity (or your computer crashes). Each call, a new instance is created which explains why you’re getting 1000s instead of just 1.

1 Like

I’d recommend using the function in a Module.

I have attempted to do so, but my module is simply printing in the output:
Requested module experienced an error while loading
+
Module code did not return exactly one value