Sound Detection Ai

new dev here
how do i go about writing an ai that can hears sounds and moves to it like the blind dogs in lethal company
the ai would work like this
when there isn’t a new sound its pathfinding
if the player lets say steps on broken glass it would attract it

so far i got the pathfinding to work but
the ai wont pick up on any new sound

i’ve looked over the forum before and couldn’t find a solution

any help is appreciated

1 Like

Try putting all the sound items in a folder.
Have the NPC check for items in the folder that are within it’s ‘hearing’ distance Magnitude.
Check to see if the sound IsPlaying.
If it is then get the NPC to go to the sound.

1 Like

Could I recommend using tags for this? Seems like an easier approach

For that, you can just see if a sound is played/added to the game by using collection service. Of course you would need to have the sounds tagged so that it doesn’t cause lag.

After that, you can just see when a sound is played using Sound.Played which is more optimal as you won’t have to use loops but because this is a pathfinding NPC then I suggest using Sound.IsPlaying in a loop.

After that you need to move the NPC to the sound, you can do this by first using Sound.Parent.Position or Sound.Parent.WorldPosition (If the parent is an attachment). Then, just use PathfindingService to move the NPC to the spot. I’d also suggest targeting the loudest and closest sound by using Sound.PlaybackLoudness which returns the loudness of the sound, thus, making it realistic.

Hope this helped.

1 Like

I wasn’t sure if a search of the entire game for tagged items would be as efficient as searching through a smaller folder for items that would be necessary to check. I guess it would also depend on whether or not these items would be added and removed from the workspace.

CollectionService:GetTagged(“Tag”) I meant😁

Just get your sounds and put it in a folder named “Sounds” then place it where ever you wish to

Make a script that iterates through each of the sound and checks their isPlaying property. if it’s true then your code will proceed with the following as you wish.

No tagging is required and is inefficient

Why would it be inefficient? Also, if I want 3D sound, I’m not gonna parent it to a folder am I?

1 Like

A disclaimer, you will not be able to write an AI and the scope of it is too broad for this forum

I would heavily recommend using CollectionService as it’s an incredibly powerful tool, and incredibly helpful to learn sooner than later.

Roblox instances contain a property called Tags, and these Tags are utilized through usage of CollectionService. I would say that the best way to go about this is to add Tags to the Sounds that you want to be able to trigger your AI, either manually through properties or :AddTag("NAMEHERE").

After this, you’d want to collect all of these tagged objects and return them as a table using CollectionService. That’s where CollectionService:GetTagged("NAMEHERE") comes in handy. It’ll return a table of all Instances that are tagged with “NAMEHERE” or what you have your tagged named as.

I’d cycle through this table and add connections to each individual sound effect, listening for the .Played event. However, this method won’t account for any new sound effects added as :GetTagged("NAMEHERE") is only called once. So a good work around for this would be to utilize CollectionService:GetInstanceAddedSignal("NAMEHERE") as this will listen for any future instances created with the tag.

If you’d like a more specific example of this, please let me know as I’d love to assist.