Jump to content

[HELP]How do i use random sounds.


NotAvailable

Recommended Posts

  • Discord Moderators

name them a number from 0 to 22

INTERVAL = 2 
function playRandomSound () 
    playSound (math.random (0, 22)..'.mp3', false) 
end 
setTimer (playRandomSound, INTERVAL * 60000, 0) 

assuming you have 22 mp3's defined with 1.mp3, 2.mp3 etc.

Edited by Guest
Link to comment

Place this where you want it:

  
local soundsTable = {} 
sounds[1] = "somemusic.mp3" 
sounds[2] = "somesound.mp3" 
-- And go on like that, until you got 22 mp3 files.  
-- Ofcourse add those sounds to your meta. 
  

And use this in your timer function:

  
local rand = math.random ( 1, #sounds ) 
playSound ( sounds[rand] ) 
  

@ Zango: That's a way too, but you will need to call your file with a number. So you have to know which number is what sound.

Edited by Guest
Link to comment

and if want you to combine the two ways: parse meta.xml nodes with mp3's into a table on resource start.

that way you wont have to edit the table when you add/delete mp3 files from meta/resource.

function getRandomSounds() 
  local t = {} 
  local meta = xmlLoadFile("meta.xml") 
  for i, node in ipairs(xmlNodeGetChildren(meta)) do 
    if xmlNodeGetName(node) == "file" and xmlNodeGetAttribute(node, "src"):find("someRandomSoundsFolder/.+\.mp3") then 
      table.insert(t, xmlNodeGetAttribute(node, "src")) 
    end 
  end 
  xmlUnloadFile(meta) 
  return t 
end 
  

Link to comment

Hmmm, its still not working

This is the code(Client):

local soundsTable = {} 
 sounds[1] = "richt1.mp3" 
 sounds[2] = "richt2.mp3" 
 sounds[3] = "richt3.mp3" 
 sounds[4] = "richt4.mp3" 
 sounds[5] = "richt5.mp3" 
 sounds[6] = "richt6.mp3" 
 sounds[7] = "richt7.mp3" 
 sounds[8] = "richt8.mp3" 
 sounds[9] = "richt9.mp3" 
 sounds[10] = "richt10.mp3" 
 sounds[11] = "richt11.mp3" 
 sounds[12] = "richt12.mp3" 
 sounds[13] = "richt13.mp3" 
 sounds[14] = "richt14.mp3" 
 sounds[15] = "richt15.mp3" 
  
  
  
  
 function randomSound() 
 local rand = math.random ( 1, #sounds ) 
 playSound ( sounds[rand], false ) 
 setSoundVolume(sounds, 0.9) 
 end 
 setTimer(randomSound, 10000, 0) 

meta.xml:

    "JESSEUNIT" version="1" type="script" /> 
    "richt1.mp3" /> 
    "richt2.mp3" /> 
    "richt3.mp3" /> 
    "richt4.mp3" /> 
    "richt5.mp3" /> 
    "richt6.mp3" /> 
    "richt7.mp3" /> 
    "richt8.mp3" /> 
    "richt9.mp3" /> 
    "richt10.mp3" /> 
    "richt11.mp3" /> 
    "richt12.mp3" /> 
    "richt13.mp3" /> 
    "richt14.mp3" /> 
    "richt15.mp3" /> 
    

Link to comment

This is how I'd have done the table:

  
local soundsTable = { 
"richt1.mp3", 
"richt2.mp3", 
"richt3.mp3", 
"richt4.mp3", 
"richt5.mp3", 
"richt6.mp3", 
"richt7.mp3", 
"richt8.mp3", 
"richt9.mp3", 
"richt10.mp3", 
"richt11.mp3", 
"richt12.mp3", 
"richt13.mp3", 
"richt14.mp3", 
"richt15.mp3", 
} 
  

You don't even have to specify the index because its added automatic. math.random(#soundsTable) to get one, and then to use it playSound(soundsTable[math.random(#soundsTable)], false)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...