So I have this dirty little bit if code I wrote up quickly to create a bullet-whooshing sound effects when a shot is fired close to a player.
local mousePos = Mouse.Hit.p;
for _, player in ipairs (game:GetService("Players"):GetChildren()) do
local plrCharacter = player.Character;
local dist = (plrCharacter.Head.Position - mousePos).Magnitude;
if player ~= Player and dist < 8 then
local s = Instance.new("Sound", player)
s.SoundId = WhooshSounds[math.random(1, #WhooshSounds)];
s:Play();
game.Debris:AddItem(s, 5);
end
end
this works, except that it is not effective for accurately detecting a shot passing close to the player since it is using mouse.Hit.p. So if a shot is fired directly passed a player, the distance is judged by wherever the mouse hit behind them (which can be thousands of studs away) which will determine the shot was not actually close enough to play the whooshing sound effects (even though the shot may have passed directly by their head).
Is there a better way to perform a check like this? I am using worldroot:raycast for the gun, so raycast:distance is not an option (I heard that Ray.new is depreciated so I avoided using it).
Somebody gave me the idea of inserting an invisible part into the player character, creating a region which the mouse could detect when clicking directly to the side or above the character, which would work, I just feel like it’s a messy way of doing it.