Number values do not equal the same in an if statement, yet are identical numbers

I’m working on a script. In the script is a function, and in said function I have a loop that repeats every 0.1 second, for a duration of time specified by the function parameter.
It works by starting a “count”, named time. Everytime it loops, it adds 0.1 to time. At the end of every loop session, it’s checked if time is the same as the timelength parameter. If it is, it returns a table, and thus, ends the function. The theory behind this is that it’ll keep adding 0.1 together, until it eventually equals the same number as timelength. In this case, the timelength is 10, so the script should end when time is the same as 10. (if you’re wondering about the coroutines, I’ve just disabled them for now until the function works as intended)

local function SwingHitbox(hitbox, timelength, multitarget, dontRepeatHit)
	print("Started")
--	local tempFunc = coroutine.create(function()
	local results = {}
	local hitboxTable = {}
	for i, v in ipairs(hitbox:GetDescendants()) do
		if v.ClassName == "Attachment" then
			table.insert(hitboxTable, v)
		end
	end
	local prevPosition = {}
	for i, v in ipairs(hitboxTable) do
		table.insert(prevPosition, v.WorldPosition)
	end
	local time = 0
	repeat
		wait(0.1)
		time = 0.1 + time
		for i, v in ipairs(hitboxTable) do
			local result = RaycastResult(prevPosition[i], v.WorldPosition, DefenseHitboxFilter, multitarget)
			if result then
				if type(result) == "table" then
					for index, value in ipairs(result) do
						if table.find(results, value) == nil then
							table.insert(results, value)
						end
					end
				elseif result.Instance then
					if table.find(results, result.Instance) == nil then
						table.insert(results, result.Instance)
					end
				end
			end
		end
		print(time)
		print(timelength)
	until time == timelength
        print("Ended count")
	return results
--	end)
--	coroutine.resume(tempFunc, hitbox, timelength, multitarget, dontRepeatHit)
end
SwingHitbox(workspace.Hitbox, 10, false)

The problem, however, is that whenever time equals 10, the same as timelength, it doesn’t consider them being equal, despite them being identical numbers. Look, here it starts counting:

StartsCounting
(prints time, and then timelength, and repeats)

But, when both numbers are 10, aka when the count should stop because they are the same numbers, it still keeps counting.

ContinuesCounting

I’ve tried a lot of debugging, but the only thing I’ve found is even weirder behaviour…
For example, apparently it works when timelength is less than 0.8:

EndsCounting
(in the screenshot above, the timelength is set as 0.5, and somehow works)

So, in short, I need help with finding out why it doesn’t work and what I can do to fix/prevent/circumvent this… “weird behaviour”.

(full script:)

--Variables

local physicsService = game:GetService("PhysicsService")
local defenseHitboxGroup = "DefenseHitbox"
multihitIgnoreList = {}

local DefenseHitboxFilter = RaycastParams.new()
DefenseHitboxFilter.FilterType = Enum.RaycastFilterType.Blacklist
DefenseHitboxFilter.FilterDescendantsInstances = multihitIgnoreList
DefenseHitboxFilter.CollisionGroup = "DefenseHitbox"



--Functions

local function collisionGroupAdd(instance, groupName)
	physicsService:SetPartCollisionGroup(instance, groupName)
end

local function RaycastResult(attach1pos, attach2pos, filter, multihit)
	if multihit == true then
		local result = nil
		while true do
			result = workspace:Raycast(attach1pos, attach2pos, filter)
			if result then
				table.insert(multihitIgnoreList, result.Instance)
				filter.FilterDescendantsInstances = multihitIgnoreList
				
			else 
				local toReturn = multihitIgnoreList
				multihitIgnoreList = {}
				filter.FilterDescendantsInstances = multihitIgnoreList
				return toReturn
			end
		end
	else
		local result = workspace:Raycast(attach1pos, attach2pos, filter)
		if result then
		end
		return result
	end
end




--Events

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local x = 0
		repeat
			local playerHitbox = Instance.new("Part", character)
			playerHitbox.Size = character.HumanoidRootPart.Size
			playerHitbox.CanCollide = false
			playerHitbox.CanTouch = false
			local weld = Instance.new("WeldConstraint", character.HumanoidRootPart)
			weld.Part0 = character.HumanoidRootPart
			playerHitbox.CFrame = character.HumanoidRootPart.CFrame
			weld.Part1 = playerHitbox
		
			collisionGroupAdd(playerHitbox, defenseHitboxGroup)
			x = x + 1
		until x == 2
	end)
end)

--[[while true do
	local result = RaycastResult(workspace.Hitbox.Attachment1.WorldPosition, workspace.Hitbox.Attachment2.WorldPosition, DefenseHitboxFilter, false)
	if result then
		print(result)
		if type(result) == "table" then
			for i, v in pairs(result) do
				print(v.Name)
			end
		else
			print(result.Instance.Name)
			end
	end
	wait(0.1)
end
--]]


local function SwingHitbox(hitbox, timelength, multitarget, dontRepeatHit)
	print("Started")
--	local tempFunc = coroutine.create(function()
	local results = {}
	local hitboxTable = {}
	for i, v in ipairs(hitbox:GetDescendants()) do
		if v.ClassName == "Attachment" then
			table.insert(hitboxTable, v)
		end
	end
	local prevPosition = {}
	for i, v in ipairs(hitboxTable) do
		table.insert(prevPosition, v.WorldPosition)
	end
	local time = 0
	repeat
		wait(0.1)
		time = 0.1 + time
		for i, v in ipairs(hitboxTable) do
			local result = RaycastResult(prevPosition[i], v.WorldPosition, DefenseHitboxFilter, multitarget)
			if result then
				if type(result) == "table" then
					for index, value in ipairs(result) do
						if table.find(results, value) == nil then
							table.insert(results, value)
						end
					end
				elseif result.Instance then
					if table.find(results, result.Instance) == nil then
						table.insert(results, result.Instance)
					end
				end
			end
		end
		print(time)
		print(timelength)
	until time == timelength
	print("Ended count")
	return results
--	end)
--	coroutine.resume(tempFunc, hitbox, timelength, multitarget, dontRepeatHit)
end
SwingHitbox(workspace.Hitbox, 0.5, false)`

I met that problem before, when I used repeat until loop, it still continues to count the numbers.

so I used for loops (not for i v in pairs)

for i = 1, 0.1 * (timelength * 10) do
   -- code
end

-- replace the repeat until code with this one

not sure if it would work but try it

1 Like

i may be wrong but i think it goes over in just a bit or something, try doing:

until time >= timelength
2 Likes

I completely forgot you could do that. I’ll try to do something like this next time I need to count in a script. Thanks!

I tried that and that works. Thanks for the easy fix!