Why isn't this parts generation code working more?

Hi Guys, so basically I was trying to make some parts generating randomly on its own for an obby. Now it generates only 2 times whereas it is required to generate 10 times.

Code -

local ObbyGenerator = {}

local partsFolder = game.ServerStorage.ObbyParts:GetChildren()

local currentPart = nil
local previousPart = nil

function ObbyGenerator:Generate()
	for i=1, 10 do
		local number = math.random(1, #partsFolder)
		local currentPart = partsFolder[number]
		currentPart.Parent = workspace
		print(currentPart.Name)
		
			if previousPart == nil then
			previousPart = currentPart
			currentPart.CFrame = CFrame.new(140, 1, 15)
		else
			currentPart.CFrame = CFrame.new(previousPart.CFrame.X, previousPart.CFrame.Y, (previousPart.CFrame.Z + previousPart.Size.Z/2) + 3)
			previousPart = currentPart
		end
	end
end

return ObbyGenerator

It is actually printing 10 times but it doesn’t generate 10 times. What’s the issue, any help is appreciated. Thanks! :slight_smile:

Instead of writing

for i=1, 10 do

write

for i=, 10 do

as with your current code it only prints 9 times.

also, why did you write this? It’s a bit confusing and it might have something to do with the generation problem.

if previousPart == nil then
	previousPart = currentPart // "This is what I'm confused about"
	currentPart.CFrame = CFrame.new(140, 1, 15)
else
	currentPart.CFrame = CFrame.new(previousPart.CFrame.X, previousPart.CFrame.Y, (previousPart.CFrame.Z + previousPart.Size.Z/2) + 3)
	previousPart = currentPart
end

You could just do the following:

local ObbyGenerator = {}

local partsFolder = game.ServerStorage.ObbyParts:GetChildren()

local currentPart = nil
local previousPart = nil

function ObbyGenerator:Generate()
	for i=0, 10 do
		local number = math.random(1, #partsFolder)
		local currentPart = partsFolder[number]
		currentPart.Parent = workspace
		print(currentPart.Name)

		if previousPart == nil then
			currentPart.CFrame = CFrame.new(140, 1, 15)
		else
			currentPart.CFrame = CFrame.new(previousPart.CFrame.X, previousPart.CFrame.Y, (previousPart.CFrame.Z + previousPart.Size.Z/2) + 3)
			previousPart = currentPart
		end
		previousPart = currentPart
	end
end

return ObbyGenerator

Tell me if something doesn’t work!

Hey, basically I wrote this because during the start of obby generation, it’s set to nil. And when the code would be generating 2nd part, it would need to add a part ahead of the previous part, so with this code, it would register a previous part and during the 2nd time of generation, it would place the part ahead. I hope you understand!

EDIT - I tried your code but it’s still the same issue :frowning:

1 Like

Ok I fixed the problem, I’m so dumb that I forgot to add :Clone().

The fixed code -

local ObbyGenerator = {}

local partsFolder = game.ServerStorage.ObbyParts:GetChildren()

local currentPart = nil
local previousPart = nil

function ObbyGenerator:Generate()
	for i=1, 10 do
		local number = math.random(1, #partsFolder)
		local currentPart = partsFolder[number]:Clone() -- I added :Clone(), because earlier it was just changing the position of folder parts and not creating new clones of them.
		currentPart.Parent = workspace
		print(currentPart.Name)
		
			if previousPart == nil then
			previousPart = currentPart
			currentPart.CFrame = CFrame.new(140, 1, 15)
		else
			currentPart.CFrame = CFrame.new(previousPart.CFrame.X, previousPart.CFrame.Y, (previousPart.CFrame.Z + previousPart.Size.Z/2) + 3)
			previousPart = currentPart
		end
		wait(1)
	end
end

return ObbyGenerator

1 Like

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