Playing a sound when a raycast passes near an enemy

Hello all, I am wanting to implement a wooshing noise that plays when a bullet (raycast) near misses you.

I simply can’t think of a way to implement this, I’ve searched for solutions and found the following How to check if a bullet is shot close to a player's head? - #4 by colbert2677 which I was hoping would answer my question however the methods use Ray.new() which afaik is deprecated and would force me to re-write a significant amount of my code or not work whatsoever.

I know it’s a lot easier with modules like fastcast but I’m stubborn and prefer to make my own stuff rather than use pre-made modules.

Thanks in advance :slight_smile:

Edit(No idea why the preformatted text is being weird)

My Code:

local rs = game:GetService(“ReplicatedStorage”)

local function bleed(hP,rR,hp)
local emitter = game.ReplicatedStorage.HitEffects.Blood:Clone()
emitter.Parent = hp
emitter.Position = hP
emitter.CFrame = CFrame.new(rR.Position, rR.Position + rR.Normal)
emitter.ParticleEmitter.Enabled = true
local weld = Instance.new(“Weld”)
weld.Parent = hp
weld.Part0 = hp
weld.Part1 = emitter
task.wait(3)
emitter.ParticleEmitter.Enabled = false
emitter:Destroy()
end

local function miss(hP,rR,hp)
local bulletimpact = rs.HitEffects.Bullethole:Clone()
bulletimpact.Parent = workspace
bulletimpact.Position = hP
local hiteffect = rs.HitEffects.HitEffect:Clone()
hiteffect.Parent = workspace
hiteffect.Position = hP
bulletimpact.CFrame = CFrame.new(rR.Position, rR.Position + rR.Normal)
hiteffect.CFrame = CFrame.new(rR.Position, rR.Position + rR.Normal)
hiteffect.Sound.Playing = true
hiteffect.ParticleEmitter.Enabled = true
task.wait(0.6)
hiteffect.ParticleEmitter.Enabled = false
task.wait(7)
bulletimpact:Destroy()
hiteffect:Destroy()
end

game.ReplicatedStorage.Remotes.FireBullet.OnServerEvent:Connect(function(player,mouse,FlashPart,dmg)

local function hitmarker()
	local hitmarker = rs.Sounds.HitMarker:Clone()
	hitmarker.Parent = player.PlayerGui
	hitmarker.Playing = true
	task.wait(2)
	hitmarker:Destroy()
end

local ig = {player.Character}
local rr = RaycastParams.new()
rr.FilterDescendantsInstances = ig
rr.FilterType = Enum.RaycastFilterType.Exclude
rr.CollisionGroup = "Default"

local range = 300
local damage = dmg
local headshotMultiplier = 7
local bodyshotMultiplier = 2
local armshotMultiplier = 1.4
local legshotMultiplier = 1.6
local footshotMultiplier = 0.1




local arms = {"RightUpperArm","RightLowerArm","RightHand","LeftUpperArm","LeftLowerArm","LeftHand"}
local legs = {"RightUpperLeg","RightLowerLeg","LeftUpperLeg","LeftLowerLeg"}
local feet = {"RightFoot","LeftFoot"}
local torso = {"UpperTorso","LowerTorso","HumanoidRootPart"}



local sP = FlashPart
local d = (mouse - sP).Unit
local rR = workspace:Raycast(sP,(mouse - sP)*range,rr)
local i = rR and rR.Position or sP + d * range
if rR then
	local hp = rR.Instance
	local hP = rR.Position
	if hp then
		print(hp.Name)
		local m = hp.Parent
		if m:FindFirstChild("Humanoid") then
			if m.Humanoid.Health <= 0 then
				local attachment = Instance.new("Attachment")
				attachment.Parent = hp
				attachment.Position = rR.Position
				local BV = Instance.new("VectorForce")
				BV.Parent = hp
				BV.Attachment0 = attachment
				BV.Force = Vector3.new(0,25,25)
				local BleedCoro = coroutine.create(bleed)
				coroutine.resume(BleedCoro,hP,rR)
				task.wait(0.25)
				BV:Destroy()
				attachment:Destroy()
				return 
			end
			if hp.Name == "Head" and m.Humanoid.Health >= 1 then
				m.Humanoid:TakeDamage(damage * headshotMultiplier)
				local hitmarkerCoro = coroutine.create(hitmarker)
				coroutine.resume(hitmarkerCoro)
				local attachment = Instance.new("Attachment")
				attachment.Parent = hp
				attachment.Position = rR.Position
				local BV = Instance.new("VectorForce")
				BV.Parent = hp
				BV.Attachment0 = attachment
				BV.Force = Vector3.new(0,25,25)
				local BleedCoro = coroutine.create(bleed)
				coroutine.resume(BleedCoro,hP,rR,hp)
				task.wait(0.25)
				BV:Destroy()
				attachment:Destroy()
				return
			end
			if table.find(torso,hp.Name) and m.Humanoid.Health >= 1 then
				m.Humanoid:TakeDamage(damage * bodyshotMultiplier)
				local hitmarkerCoro = coroutine.create(hitmarker)
				coroutine.resume(hitmarkerCoro)
				local attachment = Instance.new("Attachment")
				attachment.Parent = hp
				attachment.Position = rR.Position
				local BV = Instance.new("VectorForce")
				BV.Parent = hp
				BV.Attachment0 = attachment
				BV.Force = Vector3.new(0,25,25)
				local BleedCoro = coroutine.create(bleed)
				coroutine.resume(BleedCoro,hP,rR,hp)
				task.wait(0.25)
				BV:Destroy()
				attachment:Destroy()
				return
			end
			if table.find(arms,hp.Name) and m.Humanoid.Health >= 1 then
				m.Humanoid:TakeDamage(damage * armshotMultiplier)
				local hitmarkerCoro = coroutine.create(hitmarker)
				coroutine.resume(hitmarkerCoro)
				local attachment = Instance.new("Attachment")
				attachment.Parent = hp
				attachment.Position = rR.Position
				local BV = Instance.new("VectorForce")
				BV.Parent = hp
				BV.Attachment0 = attachment
				BV.Force = Vector3.new(0,25,25)
				local BleedCoro = coroutine.create(bleed)
				coroutine.resume(BleedCoro,hP,rR,hp)
				task.wait(0.25)
				BV:Destroy()
				attachment:Destroy()
				return
			end
			if table.find(legs,hp.Name) and m.Humanoid.Health >= 1 then
				m.Humanoid:TakeDamage(damage * legshotMultiplier)
				local hitmarkerCoro = coroutine.create(hitmarker)
				coroutine.resume(hitmarkerCoro)
				local attachment = Instance.new("Attachment")
				attachment.Parent = hp
				attachment.Position = rR.Position
				local BV = Instance.new("VectorForce")
				BV.Parent = hp
				BV.Attachment0 = attachment
				BV.Force = Vector3.new(0,25,25)
				local BleedCoro = coroutine.create(bleed)
				coroutine.resume(BleedCoro,hP,rR,hp)
				task.wait(0.25)
				BV:Destroy()
				attachment:Destroy()
				return
			end
			if table.find(feet,hp.Name) and m.Humanoid.Health >= 1 then
				m.Humanoid:TakeDamage(damage * footshotMultiplier)
				local hitmarkerCoro = coroutine.create(hitmarker)
				coroutine.resume(hitmarkerCoro)
				local attachment = Instance.new("Attachment")
				attachment.Parent = hp
				attachment.Position = rR.Position
				local BV = Instance.new("VectorForce")
				BV.Parent = hp
				BV.Attachment0 = attachment
				BV.Force = Vector3.new(0,25,25)
				local BleedCoro = coroutine.create(bleed)
				coroutine.resume(BleedCoro,hP,rR,hp)
				task.wait(0.25)
				BV:Destroy()
				attachment:Destroy()
				return
			end
		else
			local MissCoro = coroutine.create(miss)
			coroutine.resume(MissCoro,hP,rR,hp)
		end
	end
end

end)

This might also force you to rewrite alot of your code, but this is how I’d go about it (if i could atleast make a freaking proper hitscan gun that doesn’t break.)

Make another raycast. Put a bounding box/part around every player’s character, and have the one that checks if it actually hit the player pass through that bounding box. Have the other raycast not pass through the bounding box in order for it to hit the bounding box, which then you play the sound.

I thought about doing something similar to this, but it feels like it may be clunky. I can’t think of any other ways using roblox’s basic raycasting functions though. so I may just give this a try.

Alright, I got it working like was suggested by Meltlava800 I’m firing 2 rays now, one that can only collide with the collision group “Miss” and one that collides with “Default”, I spawned a large sphere around the character and welded it to their HumanoidRootPart and gave it a distinct name. When the ray hits the invisible sphere it gets the player and clones a whizz and a crack into their playerGui then removes it after 3 seconds via a coroutine to prevent hiccups in the code.

Here’s the code I ended up writing for anyone interested:

local rp = RaycastParms.new()
rp.FilterDescendantsInstances = ig --Your own character
rp.FilterType = Enum.FilterType.Exclude
rp.CollisionGroup = "Miss"

local checkMiss = workspace:Raycast(sP,(mouse - sP)*range,rp) --Fire a second ray to the same point as the first one
lf checkMiss then
    local miss = checkMiss.Instance
    if miss.Name = "NMS" then
          --Find the enemy you almost hit and play a noise for them.
    end
end

This probably isn’t even close to being the most efficient method to do this, but it’s the only way I can think of to get this effect without re-writing a substantial amount of code and doing trigonometry.

:slight_smile:

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