Troubles checking the value of i in a loop

I have a loop here, from i = 0, 1. But for some reason, I’m not able to use if statements to check the value of i over .7

Code
local module = {}

local function QuadBezier(t, p0, p1, p2)
	return (1 - t)^2 * p0 + 2 * (1 - t) * t * p1 + t^2 * p2
end

module.CreateLightning = function(startPos,finalPos,segments) --[segments is 10 bc i put 10 in the server script. So it will increment by .1]
	
	local thickness = .2 --[do u want a thicc lightning or a skinny lightning?]
	local XoffsetMin, XoffsetMAX = -1,1 --[controls how spread out it is]
	local YoffsetMIN, YoffsetMAX = -1.5,1.5 --[the bigger the range, the uglier the lightning]
	local ZoffsetMIN, ZoffsetMAX = -1,1 --[controls how spread out it is]

	
	local LerpPos = {}
	local origin = startPos
	local destination = finalPos
	local curve = (origin + destination)/2

	
	for i = 0, 1, 1/segments do
		local offset = Vector3.new(math.random(XoffsetMin,XoffsetMAX),math.random(YoffsetMIN,YoffsetMAX),math.random(ZoffsetMIN,ZoffsetMAX))
		if i == 0 or i == 1 then  --[PROBLEM OCCURS HERE. It stops detecting i when i = .7]
			print('detecting i')
			offset = Vector3.new(0,0,0) --[set it back in line, so that it ignores the offset]
		end
		LerpPos[#LerpPos + 1] = QuadBezier(i, origin,curve,destination) + offset
		print(i) --[it prints i, from 0 to 1]
	end
end


return module

It prints out i just fine, but I can’t check if i == .8 or if i == .9 or if if == 1. I tried prints as well within the loop. Like

if i == 1 then
   print('aslkjdasd')
end

but it literally won’t work??? I’m really lost someone help me. The reason I’m doing this is because I’m trying to make this lightning strike, and the part at the final position is offsetted from the final position, and I don’t want it to be.
As you can see if the photo below, the bottom part is the start position, and that’s when i = 0. And at the top, you can see it has the offset, and that’s when i = 1. I want it to be at the center of the part. Help

iirc lua loops dont actually reach their full thing you’d have to do like

for i=0,1.01, 0.01 do
 if i==1 then
   print("https://www.roblox.com/games/6567346467/Escape-The-Prison-OBBY")
 end
end

that would affect the way my lightning would look. I’ll probably have to find some way to go around this

Have you ever just considered changing it to instead of if i==1 to if i==1-0.01 ?

Yes, but it made my lightning shorter. It didn’t reach all the way to the final position

Try this out! Since the index will not be the max number, it should well be the closest:

local segments = 10

for index = 0, 1, 1/segments do
	local indexSecondClosestToOne = (1 - 1/segments)
	local indexIsClosestToOne = index > indexSecondClosestToOne
	
	if indexIsClosestToOne or index == 0 then
		print("Detected")
	end
end

Your problem is called a floating point error. Instead of using decimals, which have rounding problems, use integers.

for index = 0, segments, 1 do
    local decimal = index/segments
    if index == 8 then

This will fix several problems.

4 Likes