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.BlastRadius2 = TO_BE_SET
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.BlastRadius = 0
exp.BlastPressure = 0
exp.Position = explosionData.Position2
exp.DestroyJointRadiusPercent = 0
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
task.delay(10, function()
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)
game.Debris:AddItem(bv, 0.1)
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