Play Sound File from Entity?

Any ideas on how I can Play a specific sound file or multiple ones in .ogg format and attach it to a specific entity such as a ped like a Zombie, trying to get Zombies to have moan sound effects. I have been looking at Scott’s Interact Sound script for reference but it is confusing and does’nt really work like I want it to.

Maybe this helps? It’s not .ogg format but at least it’s a custom sound file.
https://wiki.fivem.net/wiki/Resource_manifest#data_file

I dont know how to add custom sounds in but I do know how to play it from the entity.

First you would want to create some sort of table with the entity and information. For example,

-- NPC Directory
local ShopClerk = {
{id = 1, name = "Shop Keeper", VoiceName = "SHOP_GREET", Ambiance = "AMMUCITY", modelHash = "mp_m_shopkeep_01", x = -2511.16479492188, y = 3616.90478515625, z = 13.6422147750854, heading = 245.000457763672},
}

This table holds the unique id, voice type, and audio file, and coords of the Shop Keeper.

Once you have that you would want to Spawn it.

-- Some Vars
local generalLoaded = false
local PlayingAnim = false
-- Spawning NPC
Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)
	
	if (not generalLoaded) then
	  for i=1, #ShopClerk do
        RequestModel(GetHashKey(ShopClerk[i].modelHash)) -- Gets model hash from table we made earlier
        while not HasModelLoaded(GetHashKey(ShopClerk[i].modelHash)) do
          Wait(1)
        end
		ShopClerk[i].id = CreatePed(2, ShopClerk[i].modelHash, ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, ShopClerk[i].heading, true, true)
		SetAmbientVoiceName(ShopClerk[i].id, ShopClerk[i].Ambiance) -- Sets Voice Type
        -- Add all the Ped preferences like health, etc.
      end
      generalLoaded = true
    end
	
  end
end)

Now to play a sound when you get close to them…

Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)
		RequestAnimDict("random@shop_gunstore")
		while (not HasAnimDictLoaded("random@shop_gunstore")) do 
			Citizen.Wait(0) 
		end
		
		for i=1, #ShopClerk do
			distance = GetDistanceBetweenCoords(ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, GetEntityCoords(GetPlayerPed(-1)))
			if distance < 4 and PlayingAnim ~= true then
				TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_greeting", 1.0, -1.0, 4000, 0, 1, true, true, true) -- Plays animation
				PlayAmbientSpeech1(ShopClerk[i].id, ShopClerk[i].VoiceName, "SPEECH_PARAMS_FORCE", 1) -- Makes Ped Talk from audio file we defined in the table up top
				PlayingAnim = true
				Citizen.Wait(4000)
				if PlayingAnim == true then
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
					Citizen.Wait(40000)
				end
			end
			if distance > 5.5 and distance < 6 then
				PlayingAnim = false
			end


		end
  end
end)

I hope this helps.

1 Like

thanks. But I think i would need to get it to work with using a cvustom audio file and exclude the animations part. Since I will be attaching this audio file to each Zombie that spawns in

hey man,did you ever managed to play custom audio files on peds? I need to play zombies sounds on my peds :stuck_out_tongue: