Cylinder Generator script not working

(Note I KNOW MESHES AND part.Shape exists. I’m just doing a challenge.)
So basically, i’m trying to make a script to generate cylinders, but it produces results like this:
image
what it does is it keeps making parts and rotating them until it makes a cyllinder. Here’s it being done by hand:

. Am i doing anything wrong? Here’s the script:

local Rotation = 90

function makeCircle(Radius)
	if Radius >= 1 or Radius == 1 then
		local Base = Instance.new("Part", workspace)
		Base.Size = Vector3.new(Radius,0.05,0.05)
		Base.Name = "Base"
		Base.Orientation = Vector3.new(0,0,0)
		local Base2 = Instance.new("Part", workspace)
		Base2.Size = Vector3.new(Radius,0.05,0.05)
		Base2.Name = "Base"
		Base2.Orientation = Vector3.new(0,Rotation,0)
		local BaseCross = Base:UnionAsync({Base2})
		Base:Destroy(); Base2:Destroy();
		BaseCross.Parent = workspace
		BaseCross.Name = "Base"
		Rotation = Rotation / 2
		local NewCross = BaseCross:Clone()
		NewCross.Parent = workspace
		NewCross.Orientation = Vector3.new(0,Rotation,0)
		local Degree45Finished = BaseCross:UnionAsync({NewCross})
		Degree45Finished.Parent = workspace
		Rotation = Rotation / 2
		local D45Clone = Degree45Finished:Clone()
		D45Clone.Parent = workspace
		D45Clone.Orientation = Vector3.new(0,Rotation,0)
		local Degree225Finished = Degree45Finished:UnionAsync({D45Clone})
		Degree225Finished.Parent = workspace
		Rotation = Rotation / 2
		local Degree1125 = Degree225Finished:Clone()
		Degree1125.Parent = workspace
		Degree1125.Orientation = Vector3.new(0,Rotation,0)
		local Degree1125Finished = Degree225Finished:UnionAsync({Degree1125})
		Degree1125Finished.Parent = workspace
		Rotation = Rotation / 2
		local Degree5dot625 = Degree1125:Clone()
		Degree5dot625.Parent = workspace
		Degree5dot625.Orientation = Vector3.new(0,Rotation,0)
		local Degree56Finished = Degree1125Finished:UnionAsync({Degree5dot625})
		Degree56Finished.Parent = workspace
		Rotation = Rotation / 2
		local Final = Degree56Finished:Clone()
		Final.Parent = workspace
		Final.Orientation = Vector3.new(0,Rotation,0)
		local FinishFinal = Degree56Finished:UnionAsync({Final})
		FinishFinal.Parent = workspace
		Rotation = Rotation / 2
		local ActualFinal = FinishFinal:Clone()
		ActualFinal.Parent = workspace
		ActualFinal.Rotation = Vector3.new(0,Rotation,0)
		local doneFinal = FinishFinal:UnionAsync({ActualFinal})
		doneFinal.Parent = workspace
	else
		print("Radius too small!")
	end
end
2 Likes

Is it because your Vector3.new is in Radians instead of Degrees?

It changed the orientation by 90, then makes a clone and 45, then clone and 22.5, then so on, as you can see in the video.

Yes, that’s what you did in the video, physically.
I think you’re mistaking Radians for Degrees in the script.

Alright. So how can I fix that, use CFrame.angles?

You’re mistaking the radians for degrees, you need to convert it with math.rad() iirc.

1 Like

Alright. So replace “Rotation” with math.rad(Rotation)

Also

only needs to read

   if Radius >= 1 then 

since >= means ‘greater than or equals’

I think so, I’m not the best with radians but it seems to work for me:
07:23:43.171 > print(Vector3.new(0,50,0)) - Studio
07:23:43.176 0, 50, 0 - Edit
07:23:47.231 > print(Vector3.new(0,math.rad(50),0)) - Studio
07:23:47.232 0, 0.87266463, 0 - Edit

(me assuming thats what it’s meant to do)

Yeah, the Radius == 1 is useless since >= reads both

So it just makes a very thin bowtie shape, i’m investigating the code for errors

image

Hmm, not sure why that happens tbh, I’m not great with radians so I can’t really help you unfortunately.

Ok, i’m going to mark the rads one as the answer, and investigate my code incase i made any mistake.

Oh alright, hope it works! (bro why is the limit so high when I’m giving an actual response smh)

or just set your first value as 1.5708 (radians) instead of 90 (degrees)

Wait so doing math.rad(90) at the start you mean? Ig that could work

Yeah, now I get it since you are dividing the same value

First off, you should limit the amount of CSG calculations you do because they are resource-intensive.
Also, when repeating an action it is best to use loops.

local TotalRotations = 90
local RotateDegrees = 1

function makeCircle(Radius)
	local Parts = {}
	local Rotation = 0
	while TotalRotations > 0 do
		if TotalRotations < Degrees then Degrees = TotalRotations end
		TotalRotations -= RotateDegrees

		local Part = Instance.new("BasePart")
		Part.Size = Vector3.new(Radius*2,0.05,0.05)
		Part.Orientation = Vector3.new(0,math.rad(Rotation),0)
		Part.Anchored = true
		Part.Parent = workspace

		Rotation += Degrees
		table.insert(Parts,Part)
	end

	-- We do the CSG Method after all the parts are collected:
	local Part = table.remove(Parts,1)
	local UnionCylinder = Parts:UnionAsync(Parts) -- Creates the CylinderUnion

	-- Destroy the Original Parts:
	Part:Destroy()
	for i,v in ipairs(Parts) do
		v:Destroy()
	end

	-- Parent the UnionCylinder to the workspace
	UnionCylinder.Anchored = true
	UnionCylinder.Parent = workspace
end
1 Like

Yeah this seems way more efficient rather than doing the csg for each part, you prob wanna look at this lol