Unit of a vector multiplied by a value returns nan

I am trying to make a custom explosion modulescript, however when i try to calculate the explosion knockback it returns NaN and the player vanishes. Sometimes the math is normal and sometimes it returns NaN.

The code of the modulescript:

``````local explosion = {}
local TO_BE_SET = nil

local glass = require(game.ReplicatedStorage.GunEngine.Glass)
local ragmod = require(game.ServerScriptService.R6Ragdoll.ModuleScript)

function explosion:Create()
local explosionData = {}
explosionData.BlastPressure2 = TO_BE_SET
explosionData.Position2 = TO_BE_SET
explosionData.Sound2 = TO_BE_SET
explosionData.KnockbackPower = TO_BE_SET
print("created")

return explosionData
end

local function linearDropOff(distance)
return math.clamp(-(1/140)*(distance-20)+1.5,0.3,1.5)
end

function explosion:Explode(explosionData)
local exp = Instance.new("Explosion")
exp.BlastPressure = 0

exp.Position = explosionData.Position2
exp.Visible = true
exp.Parent = workspace

print("exploded")

local part = Instance.new("Part")
part.Parent = workspace
part.CanCollide = false
part.Anchored = true
part.Transparency = 1
part.Position = explosionData.Position2
part.Name = "ExplosionSound"

for _, v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") then
local hum = v:WaitForChild("Humanoid")
local hrp = hum.Parent:WaitForChild("HumanoidRootPart")
local char = hrp.Parent

if hrp then
if (hrp.Position - part.Position).Magnitude < explosionData.BlastRadius2 then
if char ~= nil then
local dist = (hrp.Position - part.Position).Magnitude
hum.BreakJointsOnDeath = false

hum:TakeDamage(40)
char.RagdollTrigger.Value = true

char.RagdollTrigger.Value = false
end)

wait()
local knockbackDirection = (part.Position - hrp.Position).Unit * explosionData.KnockbackPower -- this is where it returns NaN
wait()
print(knockbackDirection)

local bv = Instance.new("LinearVelocity")
bv.Parent = char.Torso

local attachment = Instance.new("Attachment")
attachment.Parent = hrp

bv.MaxForce = math.huge
bv.Attachment0 = attachment
wait()
bv.VectorVelocity = knockbackDirection
wait()
print(bv.VectorVelocity)

end
end
end
end
end

local newSnd = explosionData.Sound2:Clone()
newSnd.Parent = part
newSnd:Play()
local conn

conn = newSnd.Ended:Connect(function()
conn:Disconnect()
newSnd:Destroy()
end)
end

return explosion

``````

NaN means Not a Number which is a unrepresentable number.

The math you are doing may be resulting in a number that is undefined according to the code leading to NaN appearing.

I am aware that NaN means Not a Number, but this does not really help fix the issue. The thing is that sometimes the result is NaN, and sometimes it works perfectly fine.

I canâ€™t see how KnockbackPower is calculated. Can you show us the code where it is calculated?