For loop sometimes finishes INSTANTLY

My for loop sometimes get finishes instantly


function forwardDash ()
	if isStunned() or isRagdolled() or isUsingMove() or Blocking then return end
	if (time() - LastFWDash) < 5.5 then return end
	
	
	LastFWDash = time()
	
	ForwardDashTrack:Play()
	ClientStun = true
	
	task.synchronize()
	
	local vel = Instance.new("LinearVelocity")
	

	local hrp = Humanoid.RootPart
	
	vel.Parent = hrp
	vel.Name = "ForwardDashVelocity"
	vel.ForceLimitMode = Enum.ForceLimitMode.PerAxis
	vel.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
	vel.Attachment0 = hrp:FindFirstChild("RootAttachment")

	local value = math.huge

	vel.MaxAxesForce = Vector3.new(value, 0, value)
	
	local LoopStart = time()
	
	local params = RaycastParams.new()

	params.FilterDescendantsInstances = {workspace:FindFirstChild("Players"), workspace:FindFirstChild("Dummies")}
	params.FilterType = Enum.RaycastFilterType.Include
	
	for i = 81.5, 6.5, -0.5 do

		local TimeTaken = time() - LoopStart
		local raycast = workspace:Raycast(hrp.Position, hrp.CFrame.LookVector, params)
		
		if not vel then
			ClientStun = false
			return
		end

		if raycast and raycast.Instance then
			break
		end




		if TimeTaken >= 0.855 then

			break
		end


		--if endedEarly then
		--	break
		--end


		local vector = -(hrp.CFrame.LookVector * i).Z
		local footAtt = hrp:FindFirstChild("FootAttachment")


		vel.VectorVelocity = Vector3.new(0, 0, -i)




		--warn("Completed loop iteration.")
		raycast = nil
		task.wait()

	end
	
	ForwardDashTrack:Stop()
	Events.ReplicateAnimationEnding:FireServer()

	local TimeTaken = time() - LoopStart
	
	print("Time taken: ", TimeTaken)
	
	vel:Destroy()
	ClientStun = false
	
	print("DH")
	Events:FindFirstChild("DashHit"):FireServer(hrp.Position)
	
	task.desynchronize()


	
end

image

Is there anything I can do to fix this?

From the code, the only way that it can finish instantly is if the raycast result is found instantly.

1 Like

it happens when nothing is in front of me, but maybe I should print the raycast result

It’s possible that your raycast direction is incorrect, and it’s detecting something behind it / below it / above it etc.