How can I ensure that all my lightning points will not touch each other with my current script?

Title says it all, I can’t rewrite everything because it’s too long. (Nobody would like to rewrite lots of code, right?)

Code

The main function:

function lightningmodule.Create(point1, point2, color, isStraightLine)	
	if typeof(point1) == "Instance" and typeof(point2) == "Instance" then
		point2.Orientation = point1.Orientation
		local timesToGetLightningStrike = math.random(3, 5)
		local tableOfLerps = {}
		for i = 1, timesToGetLightningStrike do
			local lerpAlpha = random:NextNumber(0, 0.9)
			local lerpCFrame = point1.CFrame:Lerp(point2.CFrame, lerpAlpha)
			if #tableOfLerps ~= 0 then
				local lastNumTable = tableOfLerps[#tableOfLerps]
				local lastnum = lastNumTable[2]
				if lerpAlpha <= lastnum and (math.abs(lerpAlpha - lastnum) >= 0.2 or (math.abs(lastnum - lerpAlpha) >= 0.2)) then
					repeat
						lerpAlpha = random:NextNumber()
						lerpCFrame = point1.CFrame:Lerp(point2.CFrame, lerpAlpha)
					until not (lerpAlpha <= lastnum) and not (math.abs(lerpAlpha - lastnum) >= 0.2 or (math.abs(lastnum - lerpAlpha) >= 0.2))
				end
				local lastTable = {lerpCFrame, lerpAlpha}
				table.insert(tableOfLerps, lastTable)
			else
				local lastTable = {lerpCFrame, lerpAlpha}
				table.insert(tableOfLerps, lastTable)
			end
		end
		local folderOfPoints = Instance.new("Folder")
		folderOfPoints.Parent = game.Workspace
		folderOfPoints.Name = "LightningPoints"
		for i, v in ipairs(tableOfLerps) do
			lightningPointCreate(1, v[1], color, isStraightLine, folderOfPoints)
		end
	elseif typeof(point1) == "Vector3" and typeof(point2) == "Vector3" then
		error("Cannot use Vector3.new(" .. point1.X .. ", " .. point1.Y .. ", " .. point1.Z .. ") and Vector3.new(" .. point2.X .. ", " .. point2.Y .. ", " .. point2.Z  .. ") as points.")
	else
		error("Cannot use " .. point1 .. " and " .. point2 .. " as points.")
	end
	if typeof(color) ~= "Color3" then
		error("Attempt to use " .. color .. " as a Color3")
	end
end

The function lightningPointCreate():

local function lightningPointCreate(amountOfPoints, cframeAmount, color, isStraight, parent)
	if color == nil then
		color = defaultColor
		if isStraight == nil then
			isStraight = true
		end
	end
	if typeof(amountOfPoints) == "number" and amountOfPoints == math.floor(amountOfPoints) then
		for i = 1, amountOfPoints do
			local point = Instance.new("Part")
			point.Size = Vector3.new(1, 1, 1)
			point.Anchored = true
			point.CFrame = cframeAmount
			point.Color = color
			point.Transparency = 0.75
			point.CanCollide = false
			point.Parent = parent
			if isStraight == false then
				local scrip = movePointScript:Clone()
				scrip.Disabled = false
				scrip.Parent = point
			end
		end
	else
		error("Attempt to use" .. amountOfPoints .. " as an integer")
	end
end

I know it’s possible, I just don’t know how.

3 Likes

I’m bumping this, it’s been 2 hours and no help yet, why.

1 Like

Try checking the distance between each lightning point, if they are within, lets say, 2 studs of each other, move it elsewhere.

Unless I’m misunderstanding what your post means, then by all means correct me.

That’s pretty much what I’m doing, problem is that I can’t find a way to check ALL points at the same time (because checking 1 point to make sure it isn’t near point 2 ignores point 3, thus creating the possibility that point 1 would be near part 3), but right now I’m figuring out how to do that. With my current code it’s kind of complicated so I need some help

Hey, I still need help with this, every solution I thought of won’t work because of what I said in my above post.

				local lastNumTable = tableOfLerps[#tableOfLerps]
				local lastnum = lastNumTable[2]
				if lerpAlpha <= lastnum and (math.abs(lerpAlpha - lastnum) >= 0.2 or (math.abs(lastnum - lerpAlpha) >= 0.2)) then
					repeat
						lerpAlpha = random:NextNumber()
						lerpCFrame = point1.CFrame:Lerp(point2.CFrame, lerpAlpha)
					until not (lerpAlpha <= lastnum) and not (math.abs(lerpAlpha - lastnum) >= 0.2 or (math.abs(lastnum - lerpAlpha) >= 0.2))
				end

This is my current code I’m using for checking if the lightning points are too near each other, doesn’t work because of the reason given in the above post.

Well, I hope I see replies tomorrow morning, I gotta go rest for the night.

Why don’t you put the checking condition inside a for loop and check say point 1 against point 2, point 3, point 4, etc? Seems like that would solve your problem. Also, I would recommend explaining what each section of your code does in more detail when you make posts for help because you will get significantly fewer responses by posting your code and saying “It doesn’t work.” Not many people like to put the effort in to help if you haven’t made much of an effort to explain. Reading code takes time and becomes much easier if you explain.

If I used ANY type of for loop, it would check 1 first. when it checks point 2, it doesn’t mind point 1, which then doesn’t solve the problem. No errors, no warnings, nothing. I thought about using task.spawn/coroutines, but I really have no idea how to use them
Time to go watch TheDevKing

Yeah it sounds like that might be the best way to go around it

My problem has been solved already, I was just thinking too hard