How do I make explosions do damage

I’m making a game, where bombs fall out of the sky and you are supposed to avoid them. While playtesting, I discovered that bombs kill you ONLY when they hit you directly, the explosion itself doesnt do any damage. I tried to change the blast radius to 100, blast pressure to 50000 and it still doesn’t work as intended. Any ideas how I can change that?

My scripts are:
Workspace - Bombscript


local Players = game:GetService(“Players”)
local bomb = script.Parent

local function onBombTouched(hit)
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)

if player then
    bomb.Anchored = true
    
    local explosion = Instance.new("Explosion")
    explosion.Position = bomb.Position
    explosion.BlastRadius = 50
    explosion.BlastPressure = 50000
    explosion.Parent = workspace
    
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        humanoid:TakeDamage(150)
    end
    
    task.wait(0.5)
    bomb:Destroy()
end

end

bomb.Touched:Connect(onBombTouched)

Replicated storage - Bomb:


local Players = game:GetService(“Players”)
local bomb = script.Parent

local function onBombTouched(hit)
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)

if player then
    -- Prevent the bomb from bouncing off the player
    bomb.Anchored = true
    
    local explosion = Instance.new("Explosion")
    explosion.Position = bomb.Position
    explosion.BlastRadius = 50
    explosion.BlastPressure = 50000
    explosion.Parent = workspace
    
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        humanoid:TakeDamage(150)
    end
    
    task.wait(0.5)
    bomb:Destroy()
end

end

bomb.Touched:Connect(onBombTouched)

2 Likes

Are you talking about the radius of the explosion registers a damage towards the near player? depending on their distance?

You can use the Hit event of the Explosion instance. This event triggers for every object within the blast radius.

Workspace - Bomb Script:

local Players = game:GetService("Players")
local bomb = script.Parent
local DamageAmount = 150

local function onBombTouched(hit)
    local character = hit.Parent
    local player = Players:GetPlayerFromCharacter(character)

    if player then
        bomb.Anchored = true

        local explosion = Instance.new("Explosion")
        explosion.Position = bomb.Position
        explosion.BlastRadius = 50
        explosion.BlastPressure = 50000
        explosion.Parent = workspace
        
        -- Handle damage when the explosion hits a character
        explosion.Hit:Connect(function(part, distance)
            local hitCharacter = part.Parent
            local hitHumanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
            if hitHumanoid then
                hitHumanoid:TakeDamage(DamageAmount)
            end
        end)
        
        task.wait(0.5)
        bomb:Destroy()
    end
end

bomb.Touched:Connect(onBombTouched)
1 Like

If you want to find all of the players within the BlastRadius of an explosion, you can use explosion.Hit.

For example,

local explosion = Instance.new("Explosion")
explosion.Position = Vector3.new(0,0,0) -- Any position
explosion.Parent = workspace

local hitConn = explosion.Hit:Connect(function(part)
	local character = part.Parent
	
	if character:FindFirstChildOfClass("Humanoid") then
		character:FindFirstChildOfClass("Humanoid").Health = 0
	end
end)

-- Disconnect the connection once you're done with it

There is also a second parameter to explosion.Hit that is the distance the part is from the explosion when it was hit. If you use this with the BlastRadius you can make the explosion do more damage if it was closer to the player, but I don’t think that’s what you’re asking for.

1 Like

Explosion.Hit is the event that you’re looking for.

local damagedistance = 50

explosion.Hit:Connect(function(part, distance)
	if distance <= damagedistance then
		local character = part.Parent
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local damage = (damagedistance - distance) / damagedistance * humanoid.MaxHealth
			humanoid:TakeDamage(damage)
			warn("Player took the damage of:\n"..damage)
		end
	end
end)
1 Like