Loop problems For loop, while true do

This is a very very basic problem. Even though this is basic I do not know how to put this in a for loop or while true do loop and make it work. So basiclly, what the script is doing is that it creates a part, sets that part to the original part rightvector and creates a random axis and angle to rotate on. But when I put this in a while true do loop or a for loop it basiclly still creates a part at the original part when what I am trying to do is create a part depending on the newPart. It is basiclly like a chain one follows the other. I need help on putting this on a while true do loop. Here is the script

local part = script.Parent

local maxRange = 50
local numberOfPartsAllowed = 5

local function increaseSize(part)
	local increment = Vector3.new(1,0,0)
	for i = 1,maxRange,1 do
		part:Resize(Enum.NormalId.Left,1)
		wait(0.01)
		if i == maxRange then
			return part
		end
	end
end


local function createPart()
	local p = Instance.new("Part",workspace)
	p.Anchored = true
	p.CanCollide = false
	p.BrickColor = BrickColor.Green()
	p.Name = "SPAWNEDPART"
	return p
end

local function getAngle()
	local minAngle = -90
	local maxAngle = 10
	
	local randomAngle = math.random(minAngle,maxAngle)
	return randomAngle
end

local function getAxis()
	local x = 1
	local y = 2
	local z = 3
	
	local randomAxis = math.random(x,z)
	return randomAxis
end

local function setUp(part,otherPart)
	local randomAxis = getAxis()
	local randomAngle = getAngle()
	
	if randomAxis == 1 then
		local RightOfPart = part.CFrame - (part.CFrame.RightVector * part.Size.X/2)
		local offSet = CFrame.new(-1.5,0,0)
		otherPart.CFrame = RightOfPart:ToWorldSpace(offSet) * CFrame.Angles(0,0,math.rad(randomAngle))
	elseif randomAxis == 2 then
		local TopOfPart = part.CFrame - (part.CFrame.UpVector * part.Size.Y/2)
		local offSet = CFrame.new(0,1.5,0)
		otherPart.CFrame = TopOfPart:ToWorldSpace(offSet) * CFrame.Angles(0,0,math.rad(randomAngle))
	elseif randomAxis == 3 then
		local FrontOfPart = part.CFrame - (part.CFrame.LookVector * part.Size.Z/2)
		local offSet = CFrame.new(0,0,1.5)
		otherPart.CFrame = FrontOfPart:ToWorldSpace(offSet) * CFrame.Angles(0,0,math.rad(randomAngle))
	end
end

--This is my attempt
for i = 1, numberOfPartsAllowed, 1 do
	if increaseSize(part) then
		local newPart = createPart()
		setUp(part,newPart)
		part = nil
		if increaseSize(newPart) then
			local newerPart = createPart()
			setUp(newPart,newerPart)
			newPart = nil
			local newpart = increaseSize(part)
		end
	end
end
1 Like

You can just put this in a part is you wanna test it out by the way

Whats the purpose of this?
Are you trying to make a tree?


The behavior seems kinda weird but it could be intended behavior, Im not sure
I just feel like if I knew what you were trying to accomplish Id be able to better assess the problem

See that’s the problem. The picture right there is something I don’t want. So I basically want a part spawning from the part that previously spawned. I don’t want the grey part to keep going. I am trying to make it so that the green parts spawn on the previous green parts

Also I am just practicing that’s why I am doing this.

image
Do you want something kinda like this?

Yes. My script already does that. The problem is the loops. I can’t seem to figure out how to make it keep going

You want it to go infinitely rather than just for a set duration?

I want to make it go for like 5 times. That is why I had the for loop there.

I couldnt get your script to work and just kinda messed with it a bit
This is the loop for the most recent image I posted if thats what you’re looking for, but I probably did something wrong

local lastPart = createPart()
increaseSize(part)
for i = 1, numberOfPartsAllowed, 1 do
	if increaseSize(part) then
		local part = createPart()
		setUp(lastPart,part)
		increaseSize(part)
		lastPart = part
	end
end

I think if I can keep playing around and switching things up using your idea I could prob get this to work. I’m getting close, the only problem now is that the base part keeps increasing even after the green parts spawn I’ll try to figure a way out to fix that. Thanks for the idea I’ll set this as a solution

1 Like

I got it to work thanks to your solution. Thank you for the help! Really appreciate!

Updated Code:

local part = script.Parent

local maxRange = 50
local numberOfPartsAllowed = 10

local function increaseSize(part)
	local increment = Vector3.new(1,0,0)
	for i = 1,maxRange,1 do
		part:Resize(Enum.NormalId.Left,1)
		wait(0.01)
		if i == maxRange then
			return i
		end
	end
end


local function createPart()
	local p = Instance.new("Part",workspace)
	p.Anchored = true
	p.CanCollide = false
	p.BrickColor = BrickColor.Green()
	p.Name = "SPAWNEDPART"
	return p
end

local function getAngle()
	local minAngle = -90
	local maxAngle = 10
	
	local randomAngle = math.random(minAngle,maxAngle)
	return randomAngle
end

local function getAxis()
	local x = 1
	local y = 2
	local z = 3
	
	local randomAxis = math.random(x,z)
	return randomAxis
end

local function setUp(part,otherPart)
	local randomAxis = getAxis()
	local randomAngle = getAngle()
	
	if randomAxis == 1 then
		local RightOfPart = part.CFrame - (part.CFrame.RightVector * part.Size.X/2)
		local offSet = CFrame.new(-1.5,0,0)
		otherPart.CFrame = RightOfPart:ToWorldSpace(offSet) * CFrame.Angles(0,0,math.rad(randomAngle))
	elseif randomAxis == 2 then
		--local TopOfPart = part.CFrame - (part.CFrame.UpVector * part.Size.Y/2)
		local RightOfPart = part.CFrame - (part.CFrame.RightVector * part.Size.X/2)
		local offSet = CFrame.new(0,1.5,0)
		otherPart.CFrame = RightOfPart:ToWorldSpace(offSet) * CFrame.Angles(0,0,math.rad(randomAngle))
	elseif randomAxis == 3 then
		--local FrontOfPart = part.CFrame - (part.CFrame.LookVector * part.Size.Z/2)
		local RightOfPart = part.CFrame - (part.CFrame.RightVector * part.Size.X/2)
		local offSet = CFrame.new(0,0,1.5)
		otherPart.CFrame = RightOfPart:ToWorldSpace(offSet) * CFrame.Angles(0,0,math.rad(randomAngle))
	end
end

local lastpart = createPart()
lastpart.Transparency = 1
increaseSize(part)
setUp(part,lastpart)

--This is my attempt
--for i = 1, numberOfPartsAllowed, 1 do
	--if increaseSize(part) == maxRange then
		--break
	--end
	--local newpart = createPart()
	--setUp(lastpart,newpart)
	--increaseSize(newpart)
	--lastpart = newpart
--end

if increaseSize == maxRange then
	print("Max, returning")
	return true
end

for i = 1,numberOfPartsAllowed,1 do
	local newpart = createPart()
	setUp(lastpart,newpart)
	increaseSize(newpart)
	lastpart = newpart
end