How do I make my for loop not repeat already done actions?

Hi. I’m not sure how to properly explain my problem but I will try my best.

Here is a gif of what my script does.
https://gyazo.com/f2125abb54f9f09b56772114a39b9f0a

But if you notice, there is a delay which becomes more noticeable as the script runs. I’ve figured out that it is simply because the script is drawing lines which have already been drawn. I have thought about how to fix this for a while but have come up with nothing. Does anyone know how to fix this?

local currebtP = workspace["1"].Position  -- current position
local currentA = 0  -- current angle
local num = 1   -- number of points
local points = {}

workspace["1"].Transparency = 1
workspace["1"].Size = Vector3.new(.03,.03,.03)


function createLine(pos1, pos2) -- draw line between two positions
	local line = Instance.new("Part")
	line.Anchored = true
	line.CanCollide = false
	line.Parent = workspace
	line.Material = Enum.Material.Neon
	line.Name = 'line'
	line.TopSurface = "SmoothNoOutlines"
	line.BottomSurface = "SmoothNoOutlines"
	
	
	local distance = (pos1 - pos2).magnitude
	line.Size = Vector3.new(0.01, 0.01, distance)
	line.CFrame = CFrame.new(pos1, pos2) * CFrame.new(0, 0, -distance / 2)
end

function createPoint(radius, line) -- (distance between points, draw line between points?[true/false])
	num = num + 1
	
	local part = Instance.new("Part", workspace)
	part.Name = num
	part.Size = Vector3.new(.03,.03,.03)
	part.BrickColor = BrickColor.new("Baby blue")
	part.Material = Enum.Material.Neon
	part.Anchored = true
	
	table.insert(points, part)
	
	local angle = math.rad(currentA)
	local origin = currebtP
	local newP = origin + Vector3.new(math.sin(angle)*radius,0,math.cos(angle)*radius)
	part.Position = newP
	
	currebtP = newP
	
	if line == true then
		createLine(workspace[tostring(num-1)].Position, workspace[tostring(num)].Position) -- connect previous point and current point with line
	end

	print(newP)
end

function turn(x)
	currentA = currentA + x
end

sides = 15   -- pick polygons number of sides
exteriorAngle = 360/sides
print(exteriorAngle)

for x = 1,sides do   -- Create polygon
	wait()
	createPoint(sides/(sides/3), true)
	turn(exteriorAngle)
end

for k,v in pairs(points) do -- connect all corners to all other corners with line
	for k,x in pairs(points) do
		
		wait()
		createLine(v.Position, x.Position)
	end
end

print("done")

Sorry if messy.

Do you have a While True do Statement, because if you do, then it might be repeating over & over again?

Another reason for this is maybe you need a couple of more lines to stop the action before ending the script so that it wouldn’t start over & over again. I don’t know the line to stop an action, but try to make sure that it won’t go one forever without stopping.

That is what I know so far, I’ll update this in case I might know more about this cause, I hope I answered your question!

Note; I would see how to find out how to stop a looping script with parts, anyway, I hope I helped!

It does stop. My problem is that the for loop draws lines which already exist.

Then for that, it should detect that:

If the line fragment has already been drawn or inserted, then either stop drawing on that certain area & find one that hasn’t been made

Because if it does already exist, then it has to stop, and if the line doesn’t have that certain lines, then it will think there isn’t a line, then it will draw over it. So I think the script you have should detect the line so it won’t overlap or any weird patterns.

I hope I helped a little since I haven’t really know how to stop parts from forming over another part.

You just explained my question to me in a slightly different way than I said.

Then I really don’t have a answer for that, sorry dude.

:confused:

I believe simply reducing the number of points it loops through each times should fix it.

I can’t test it right now due to the Roblox Server issues, but try this:

workspace["1"].Transparency = 1
workspace["1"].Size = Vector3.new(.03,.03,.03)


function createLine(pos1, pos2) -- draw line between two positions
	local line = Instance.new("Part")
	line.Anchored = true
	line.CanCollide = false
	line.Parent = workspace
	line.Material = Enum.Material.Neon
	line.Name = 'line'
	line.TopSurface = "SmoothNoOutlines"
	line.BottomSurface = "SmoothNoOutlines"
	
	
	local distance = (pos1 - pos2).magnitude
	line.Size = Vector3.new(0.01, 0.01, distance)
	line.CFrame = CFrame.new(pos1, pos2) * CFrame.new(0, 0, -distance / 2)
end

function createPoint(radius, line) -- (distance between points, draw line between points?[true/false])
	num = num + 1
	
	local part = Instance.new("Part", workspace)
	part.Name = num
	part.Size = Vector3.new(.03,.03,.03)
	part.BrickColor = BrickColor.new("Baby blue")
	part.Material = Enum.Material.Neon
	part.Anchored = true
	
	table.insert(points, part)
	
	local angle = math.rad(currentA)
	local origin = currebtP
	local newP = origin + Vector3.new(math.sin(angle)*radius,0,math.cos(angle)*radius)
	part.Position = newP
	
	currebtP = newP
	
	if line == true then
		createLine(workspace[tostring(num-1)].Position, workspace[tostring(num)].Position) -- connect previous point and current point with line
	end

	print(newP)
end

function turn(x)
	currentA = currentA + x
end

sides = 15   -- pick polygons number of sides
exteriorAngle = 360/sides
print(exteriorAngle)

for x = 1,sides do   -- Create polygon
	wait()
	createPoint(sides/(sides/3), true)
	turn(exteriorAngle)
end

local connectedPoints = -1 -- starts at -1, so that in the first loop it'll start at 0

for k,v in pairs(points) do -- connect all corners to all other corners with line

        connectedPoints = connectedPoints + 1 -- increase the amount of connected points

	for i=1, #points-connectedPoints do -- loops 1 to # of points - connectedPoints
		
		wait()
		createLine(v.Position, points[i].Position) -- instead of x, points[i]

	end
end

print("done")

edit: also, like @PirateOnThePlank said, you should move this to #development-support:scripting-support

First of all, to me it seems like the speed is still the same for all points, it’s just that the absence of visual confirmation of each drawn line is what causes you to think it’s slower, the script moves from one point to another at the same speed.
However, I assume you want the script to form the polygon as fast as possible, so here goes.

Addressing the problem: Premade Connections


A line drawn to A from B can either be thought of AB or BA, meaning the same line corresponds to 2 lines, one from each point. To avoid making two lines in the same place, we can attempt to save which connections have already been made

local lines = {}
for _, p1 in pairs (points) do
    for _, p2 in pairs(points) do
        if not p2 == p1 then -- Don't draw line to itself
            if not lines[p2] or not lines[p2][p1] then -- We check for lines
                -- we need to check for only p2 -> p1 and not for p1 -> p2 as well 
                -- since they are either both there or none are
                createLine(p1.Position, p2.Position)
                -- Register new line
                lines[p1][p2] = true
                lines[p2][p1] = true
            end
        end
    end
end

I haven’t tested this script but I believe it would work right, on another note, please shift those post to #development-support:scripting-support

2 Likes

This made it only begin at the first 10 or so corners then stop before it reached the end.

I tried this and it just skips the entire for loop straight to the “done”.

Try replacing

createLine(v.Position, points[i].Position)

with

createLine(v.Position, points[#points-i].Position)

error: Workspace.Script:75: attempt to index field ‘?’ (a nil value)

Whoops, assumed that CreateLine() takes parts, not positions. Made a change in the previous script to reflect the same

The script still just skips right over the loop. Not sure why

Is it throwing an error at lines[p1][p2] = true?
It would throw something like attempt to index a nil value
Here’s another fix

local lines = {}
for _, p1 in pairs (points) do
    lines[p1] = {}
    for _, p2 in pairs(points) do
        if not lines[p2] then
            lines[p2] = {}
        end
        if not p2 == p1 then -- Don't draw line to itself
            if not lines[p2] or not lines[p2][p1] then -- We check for lines
                -- we need to check for only p2 -> p1 and not for p1 -> p2 as well 
                -- since they are either both there or none are
                createLine(p1.Position, p2.Position)
                -- Register new line
                lines[p1][p2] = true
                lines[p2][p1] = true
            end
        end
    end
end

No error. Still just skips right passed it.

Can you try adding something like this so that we can see what it’s trying to index?

print(points[#points-i])
print([#points-i])

This is probably a simple issue, I’ve done things like this before, but it’s just not coming to me right now.