--------

Thread:Spawn(function()
	while true do
		for Index, Object in ipairs(ActiveHitboxes) do
			if Object.deleted then
				Handler:remove(Object.object)
			else
				for _, Point in ipairs(Object.points) do
					if not Object.active then
						Point.LastPosition = nil
					else
						local rayStart, rayDir, RelativePointToWorld, debugPart = Point.solver:solve(Point, game:GetService("ReplicatedStorage"):WaitForChild("ClientSettings").Debug.Value)

						local function castFunction(rayStart, rayDir, params)
							local resultOfCast = workspace:Raycast(rayStart, rayDir, params)

							if resultOfCast then
								local hitInstance = resultOfCast.Instance

								if hitInstance.Parent:IsA("Accoutrement") then
									table.insert(params.FilterDescendantsInstances, hitInstance)
									resultOfCast = nil
								end
							end

							return resultOfCast
						end

						local raycastResult = castFunction(rayStart, rayDir, Object.raycastParams)
						Point.solver:lastPosition(Point, RelativePointToWorld)

						if raycastResult then
							local hitPart = raycastResult.Instance
							local findModel = not Object.partMode and hitPart:FindFirstAncestorOfClass("Model")
							local humanoid = findModel and findModel:FindFirstChildOfClass("Humanoid")
							local target = humanoid or (Object.partMode and hitPart)

							if target and not Object.targetsHit[target] then
								Object.targetsHit[target] = true
								
								if debugPart ~= nil and typeof(debugPart) == "Instance" then
									debugPart.Color = Color3.fromRGB(85, 255, 0)
								end
								
								Object.OnHit:Fire(hitPart, humanoid, raycastResult, Point.group)
							end
						end

						if Object.endTime > 0 then
							if Object.endTime <= clock() then
								Object.endTime = 0
								Object:HitStop()
							end
						end

						Object.OnUpdate:Fire(Point.LastPosition)
					end
				end
			end
		end
		
		Thread:Wait(0.028)
	end
end)

return Handler
