Making a faster hit/touch detection

Basically, I’ve created a bomb that allows you to throw it in the direction of the player’s mouse and once the bomb comes in contact with a part or object, it fires a ‘blow up’ function.

Problem
The touched event is a little delayed where the blowup function doesn’t fire on impact, but instead a few milliseconds afterward.
I need it so that the explosion happens instantaneously to get a smoother experience when the bomb touches something.
I’m not sure if the touched event is used incorrectly or if I need to use something else for this situation.

From this video, you can still see the bomb fly through the wall before the explosion event fires.

"Bomb" script that fires the “blowUp” function:

local explodeEvent = game.ReplicatedStorage:WaitForChild("ItemModules"):WaitForChild("BombModule"):WaitForChild("ExplodeEvent")

local FuseSound = Instance.new("Sound")
FuseSound.SoundId = "http://www.roblox.com/asset/?id=11565378"
FuseSound.Volume = 1
FuseSound.Name = "FuseSound"
FuseSound.Parent = script.Parent
FuseSound:Play()

--Explosion Variables
local canExplode = false
local blastRadius = 20
local blastPressure = 100000
local damage = 100

script.Parent.CanCollide = false

--Local Functions--
local function explodeOnTouch()
	if canExplode == true then
		canExplode = false
		blowUp()
	end
end

--Global Functions--
function blowUp()
	script.Parent.Transparency = 1
	
	local explosion = Instance.new("Explosion")
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastRadius = blastRadius
	explosion.BlastPressure = blastPressure
	
	explodeEvent:FireAllClients()
	

	-- find instigator tag
	local creator = script.Parent:findFirstChild("creator")
	if creator ~= nil then
		explosion.Hit:connect(function(part, distance)
			onPlayerBlownUp(part, distance, creator)
		end)
	end

	explosion.Position = script.Parent.Position
	explosion.Parent = game.Workspace
	
	
	local bombFire = script.Parent.FireAttachment.Fire
	bombFire.Enabled = false
	
	local ExplodeSound = Instance.new("Sound")
	ExplodeSound.SoundId = "http://www.roblox.com/asset/?id=142070127"
	ExplodeSound.Volume = 1
	ExplodeSound.Name = "ExplodeSound"
	ExplodeSound.Parent = script.Parent
	ExplodeSound:Play()
end

function onPlayerBlownUp(part, distance, creator)
	if part.Name == "Head" then
		local humanoid = part.Parent.Humanoid
		if humanoid and not humanoid:IsDescendantOf(script.Thrower.Value.Character) then
			humanoid:TakeDamage(damage)
			tagHumanoid(humanoid, creator)
		end
	end
end

function tagHumanoid(humanoid, creator)
	-- tag does not need to expire if all explosions lethal
	if creator ~= nil then
		local new_tag = creator:clone()
		new_tag.Parent = humanoid
	end
end

function untagHumanoid(humanoid)
	if humanoid ~= nil then
		local tag = humanoid:findFirstChild("creator")
		if tag ~= nil then
			tag.Parent = nil
		end
	end
end

wait(.1)
canExplode = true

script.Parent.Touched:Connect(explodeOnTouch)

game.Debris:AddItem(script.Parent,5)

As far as I can see you don’t destroy the bomb on impact, you only destroy it 5 seconds after creating, which is not realistic, and which might be causing your issue. Visually I see no delay at all, yes, we can see the bomb flying through the wall, but again, it’s because the bomb is flying very fast and you don’t destroy it on impact.

However there are always some inconsistencies/delays with projectiles on client and server. The reason is Network Ownership. It basically determines where are the physics calculated, client or server. By default the network owner is server, and gets changed to client once it gets too close to client.

The reason of delay might be, that on client you see the bomb reach the wall, but on server it yet hasnt, because of ping. So you can set the network ownership of your bomb to the client using SetNetworkOwner().

2 Likes