Projectile explodes when touches anything

hello, I made a projectile but wondering how to make it explode when touched something
Heres the serverScript:


local function lerp(p0,p1,t)
	return p0*(1-t) + p1*t
end


local function quad(p0,p1,p2, t)
	local l1 = lerp(p0,p1,t)
	local l2 = lerp(p1,p2,t)
	local quad = lerp(l1,l2,t)
	return quad
end


game.ReplicatedStorage.Remotes.LightAttackProjectileEvent.OnServerEvent:Connect(function(player, mousehit, hrpPos)
	local Char = player.Character
	local Hum = Char:WaitForChild("Humanoid")
	local start = hrpPos + Vector3.new(0,4,0)
	local finish = mousehit.Position
	local Middle = (finish - start) + mousehit.Position - Vector3.new(0,-15,-30) - (finish - start)
	local Light = game.ReplicatedStorage.LightAttackProjectile:Clone()
	Light.Parent = workspace
	local sound = Instance.new("Sound")
	sound.SoundId = "rbxassetid://1566260295"
	sound.Parent = Light
	print("cloned")
	sound:Play()
	local Animation = Instance.new("Animation")
	Animation.AnimationId = "rbxassetid://12164163337"
	local LoadedAnimation = Hum:LoadAnimation(Animation):Play()
	for i = 1, 40 do
		local t = i/40
		local updated = quad(start,Middle,finish,t)
		Light.Position = updated
		wait(0.01)
		end
	wait(3)
	Light.Transparency = 1
	Light.CanCollide = false
	Light.ParticleEmitter.Enabled = false
	Light.PointLight.Enabled = false
	Light.Trail.Enabled = false
	Light.BillboardGui.ImageLabel.Visible = false
	local Ball = Instance.new("Part", workspace)
	Ball.Material = Enum.Material.Neon
	Ball.BrickColor = BrickColor.new(255,255,0)
	Ball.Size = Vector3.new(2,2,2)
	Ball.Position = Light.Position
	Ball.Anchored = true
	Ball.CanCollide = false
	Ball.Transparency = 0
	Ball.Shape = "Ball"
	local ExplosionSound = Instance.new("Sound", Ball)
	ExplosionSound.SoundId = "rbxassetid://9117894969"
	ExplosionSound.Volume = 1.5
	ExplosionSound.RollOffMode = Enum.RollOffMode.LinearSquare
	ExplosionSound.RollOffMaxDistance = 50
	ExplosionSound.RollOffMinDistance = 5
	ExplosionSound:Play()
	local TS = game:GetService("TweenService")
	local TI = TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local T1 = TS:Create(Ball, TI, {Size = Vector3.new(12,12,12)})
	local T2 = TS:Create(Ball, TI, {Transparency = 1})
	T1:Play()
	T2:Play()
	task.wait(1)
	Ball:Destroy()
	Light:Destroy()
end)```
I made it explode after couple of seconds but trying to get it explode on touch 
And if humanoid touched it should take 10 damage per explosion
Any help?
1 Like

Use the BasePart.Touched signal, then you want to disconnect anything like inside the for loop you want to have the following statement:

if not projectile then break end

Then you want to run the Touched event:

projectile.Touched:Connect(function()
   local explosion = instance.new("Explosion")
   explosion.Position = projectile.Position
end)

To change any of the properties read this: Explosion | Roblox Creator Documentation

1 Like

Hey, Thanks it works but how can I set a damage
Beacause I dont want the player to die instantly

1 Like

I don’t see where your explosion is, but if you want the explosion itself to do 0 damage, set Explosion.BlastPressure = 0. That will stop the damage. Furthermore, to make the humanoid take damage, use the Humanoid.TakeDamage(damage) method. By using take damage, it takes into effect if the player has a force field or not protecting them.

If this worked please put a message as solution so that this post gets shelved

I recommend using a spatial query like GetPartBoundsInBox and GetPartBoundsInRadius, Raycast won’t work for this case since you want a projectile to explode on contact.

GetPartBoundsInBox is a box that checks if any bounding box overlaps with the box, it’s not very accurate because it doesn’t consider shapes such as capsules, wedges and triangles.

If you want to see a part’s bounding box, you should click on the part and look at the outline of the selected part, it will always be a square.

In summary, GetPartBoundsInBox is perfect for explosions and sci-fi blasts.

Here’s a example of using GetPartBoundsInBox for projectile explosions:

local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Blacklist
overlapParams.FilterDescendantsInstances = {}

local projectile = Instance.new("Part")
projectile.Anchored = true
projectile.Size = Vector3.one * 0.75
projectile.Position = Vector3.one
projectile.CanCollide = false
projectile.CanQuery = false -- CanQuery is really important for spatial queries, whenever it's off, it will be "blacklisted".
projectile.Name = "Projectile"
projectile.Parent = workspace

local projectileCFrame = projectile.CFrame

local projectileSpeed = 5

local ExplosionSize = Vector3.one * 15
local ExplosionBaseDamage = 50 -- ExplosionBaseDamage is the base damage of the explosion.
local DirectHitDamage = 25 -- DirectHitDamage is whatever player was hit by the projectile, similar to Team Fortress 2 Grenade pipes.

local Heartbeat 
Heartbeat = game:GetService("RunService").Heartbeat:Connect(function(dt)
	local speedFrame = projectileSpeed * dt
	local currentProjectileCFrame = projectile.CFrame
	local direction = currentProjectileCFrame.LookVector -- LookVector, UpVector, RightVector are always unit vectors.
	
	local newProjectileCFrame = currentProjectileCFrame + (direction * speedFrame)
	
	projectile.CFrame = newProjectileCFrame
	
	local ProjectileIntersection = workspace:GetPartBoundsInBox(newProjectileCFrame, projectile.Size, overlapParams)
	
	if ProjectileIntersection and next(ProjectileIntersection) then
		-- The projectile intersected with part, we will do our explosion.
		-- We are going to loop through ProjectileIntersection to get whatever player is hit directly by the projectile.
        -- next(ProjectileIntersection) checks if the array is empty or not.
		
		local CharacterHit = nil
		for i, part in ProjectileIntersection do
			local ParentOfPart = part.Parent
			if not ParentOfPart then continue end
			
			local Humanoid = ParentOfPart:FindFirstChildOfClass("Humanoid")
			if not Humanoid or Humanoid and Humanoid.Health <= 0 then continue end
			
			CharacterHit = ParentOfPart
			break
		end
		
		local ExplosionIntersection = workspace:GetPartBoundsInBox(newProjectileCFrame, ExplosionSize, overlapParams)
		
		local AlreadyHitParts = {}
		-- AlreadyHitParts will prevent an explosion from hitting a player more than once.
		for i, hitPart in ExplosionIntersection do
			local ParentOfPart = hitPart.Parent
			if not ParentOfPart then continue end
			
			if table.find(AlreadyHitParts, ParentOfPart) then continue end
			
			local Humanoid = ParentOfPart:FindFirstChildOfClass("Humanoid")
			if not Humanoid or Humanoid and Humanoid.Health <= 0 then continue end

			local Distance = (hitPart.Position - newProjectileCFrame.Position).Magnitude
			local Damage = math.clamp(ExplosionBaseDamage - Distance, 0, ExplosionBaseDamage)
			if CharacterHit and ParentOfPart == CharacterHit then Damage += DirectHitDamage end
			print("Damage: " .. Damage)
			
			Humanoid.Health -= Damage
			table.insert(AlreadyHitParts, ParentOfPart)
		end
		
		Heartbeat:Disconnect()
		Heartbeat = nil
		
		projectile:Destroy()
		script:Destroy()
	end
end)
2 Likes

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