"Calling task.delay(t, f) where t is either NaN or infinite will cause f to be called with a timeout of 0."

hi. so, im trying to make a pretty straightforward ability module. you dash, and if you dont collide with an object, you stop after .5 seconds. if you do collide with an object, you stop either way, just WAY earlier.
everything seems to work fine, but for some reason, line 46 keeps spamming warnings (see title) everytime you collide with an object, which causes insane lag issues.

here’s the script:

local module = {['boundData'] = {
	dash_speed = 128;
	first_dash_duration = 0.5;
};

	{['boundHitboxData'] = {
		size = Vector3.new(6.3, 5.1, 3);
	} }

}

local function Dash(p, ms)
	local debounce = false

	if debounce == false then
		debounce = true
		local timer

		spawn(function()
			local opt = RaycastParams.new()
			opt.FilterType = Enum.RaycastFilterType.Exclude
			opt.FilterDescendantsInstances = {p}
			
			
			local res = workspace:Raycast(p.Torso.Position, p.Torso.CFrame.LookVector * 30, opt)
			
			local bv = Instance.new("BodyVelocity", p.HumanoidRootPart)
			local ao = Instance.new("AlignOrientation", p.HumanoidRootPart)

			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bv.Velocity = p.Torso.CFrame.LookVector * module.boundData.dash_speed
			bv.P = math.huge

			ao.RigidityEnabled = true
			ao.Mode = Enum.OrientationAlignmentMode.OneAttachment
			ao.Attachment0 = Instance.new("Attachment", p.HumanoidRootPart, p.Torso)
			ao.PrimaryAxis = Vector3.new(1, 0, -0.017)
			ao.SecondaryAxis = Vector3.new(0,1,0)
			ao.LookAtPosition = Vector3.new(0,0,0)
			ao.CFrame = CFrame.new(0,1,0)
			ao.Responsiveness = math.huge
			
			while game["Run Service"].Heartbeat:Wait() do
				
				if res and res.Instance and res.Normal then
					task.wait((p.Torso.Position / res.Normal).Magnitude) -- line 46
					bv:Destroy()
					ao:Destroy()
				else
					task.wait(0.5)
					bv:Destroy()
					ao:Destroy()
				end
			end
		end)
		debounce = false
	end
	return
end

function module.Bound(player, ms)
	Dash(player, ms)
end

return module

I have to say, it seems like every time you dash you start a new infinite loop that never ends. This is probably not what is causing the errors, but it’s definitely a bad idea.

you can check for NaN by seeing if the value is not equal to itself:
local isNan = (a ~= a)

what does that delay based on the divison of the torso position and the hit normal even do though? I’ve been scratching my head over that, it seems like nonsense to me

basically when you look at an object, it calculates how much time youre gonna spend dashing over to it and colliding with it, so when you do reach it, you stop moving immediately. i couldnt really come up with a better idea on how to pull that off

Think about the definition of distance traveled:

d = s * t this states that the distance traveled (d) is equal to the travel speed (s) multiplied by the amount of time traveling (t)

using simple algebra - dividing both sides by “s” - you can solve for the time it takes to travel a distance at a certain travel speed:
d / s = t

now you have a way to calculate time using a distance and speed. You have the travel speed in your code as module.boundData.dash_speed, so now you only need to get the distance between the player and their target position (which is provided by res.Distance).

from what I can see, you should be able to safely replace that while block with the following code (you should not need the loop):

game:GetService("RunService").Heartbeat:Wait() -- wait for the heartbeat once

local dashDelay = 0.5
	
if res and res.Hit then -- calculate new delay if the ray hits something
    dashDelay = res.Distance / module.boundData.dash_speed
end

task.wait(dashDelay)
bv:Destroy()
ao:Destroy()

let me know if it helps or just gives errors

it works, but only to an extent

you only stop if you’re 10 or less studs away from the object youre trying to connect with, so for example, if youre too far away, you’ll just keep sprinting into it until the default dash duration ends.

Is that the expected behaviour or what the script is doing?

If it is raycasting too far ahead (causing abnormally long dashing), that is because of the * 30 you have multiplied to the ray direction. This should actually be the maximum distance traveled by the player with dashing (10 studs, from what you just said)

nevermind, i think i figured it out.
i used the script you gave me earlier and changed the lookvector multiplier from 10 to 60, works as intended now.
thanks man :slight_smile:

1 Like

So what is the script doing currently? It should be doing that, it waits for the heartbeat, sets the default delay, only changes that delay if there is an object to run into, and then waits for that delay before deleting the velocity. It should do exactly as you say (a loop shouldnt be needed because you are only waiting to remove the velocity once that is needed)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.