Explosion.hit running even though distance is over blast radius

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want the bomb I’ve made, which explodes on touch, to damage any nearby characters.
  2. What is the issue? Include screenshots / videos if possible!
    The explosion.hit event used to detect the explosion fires even though the parts hit are Outside the given blast radius, resulting in causing negative damage and giving the explosion target health instead.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I thought of setting the distance to the blast radius if it was larger than the blast radius but my then damage script, which runs on my ServerScriptService, would transfer 0 damage even though the part was hit.

This is the code that the bomb uses. It explodes when touching any basepart. Basically this code is taken from the explosion.hit explanation page, except the damage system, which fires a bindable event instead because I’m using a multiple-types-of-damage system. (The bomb has a 1/5 chance of setting people on fire.)


local bomb = script.Parent
local event = game.Workspace.BindableEvents.RequestDamage
local effect = game.Workspace.BindableEvents.RequestEffect

bomb.Touched:Connect(function()
    local explosion = Instance.new("Explosion")
    explosion.BlastPressure = 0
    explosion.BlastRadius = 7
    explosion.DestroyJointRadiusPercent = 0
    explosion.Position = bomb.Position + Vector3.new(0,-1,0)
    local modelsHit = {}
    explosion.Hit:Connect(function(part , distance)
        local parentModel = part.Parent
        if modelsHit[parentModel] then
            return
        end
        modelsHit[parentModel] = true
        local humanoid = parentModel:FindFirstChild("Humanoid")
        if humanoid then
            local distanceFactor = distance/explosion.BlastRadius
            distanceFactor = 1 - distanceFactor
            event:Fire(math.floor(40*distanceFactor),"Physical",humanoid.Parent)
            if math.random(1,5) == 5 then
                effect:Fire("Burn",humanoid.Parent)
            end
        end
    end)
    explosion.Parent = workspace
    wait()
    bomb:Destroy()
end)

1 Like

This issue will now be solved using the crude method of setting an if statement to completely cancel any damage request if distance is over blast radius.

To any developers that might have found this post searching for a solution regarding the problem above, I’ve realised it is probably more wise to restrict the code from running using a if statement like so:

if distance < blast_radius then
    - - Code here
end
2 Likes