Jump to content

play random sound


Xwad

Recommended Posts

hi im trying to make a script that will play a random sound when i type "test". the script is not working pls help.

sound = {"files/shoot.wav"}, {"files/shoot2.wav"} 
  
sounds = sound [ math.random ( #sound ) ] 
  
function random_sound() 
    local shoot = playSound(sound)  
    setSoundVolume(shoot, 10)  
end 
  
addCommandHandler ( "test", random_sound ) 

Link to comment
sound = {"files/shoot.wav"}, {"files/shoot2.wav"} 

That won't work, it is correct but it won't work in the way you want it to.

Should be like this:

sound = {"files/shoot.wav", "files/shoot2.wav"} 

Also,

function random_sound() 
    local shoot = playSound(sound) 
    setSoundVolume(shoot, 10) 
end 

You are trying to play a table, should be sounds as you defined above.

And setSoundVolume takes a volume in the range 0 - 1.

Link to comment

fixing argumentum problems was always difficult for me..pls help!

Bad 'sound/player' pointer @ setSoundVolume'(1)

Bad Argumentum @ 'playSound' [Expected string at argument 1,got table]

sound = {"files/shoot.wav", "files/shoot2.wav"} 
  
sounds = sound [ math.random ( #sound ) ] 
  
function random_sound() 
    local shoot = playSound(sound) 
    setSoundVolume(shoot, 1) 
end 
  
addCommandHandler ( "go", random_sound ) 

Link to comment
  • 4 months later...

and why is this one not working?

sounds = {"files/fire1.wav", "files/fire2.wav", "files/fire3.wav"} 
sound = sounds [ math.random ( #sounds ) ] 
  
fired = false 
  
function projectile_sound(veh)           
local x,y,z = getElementPosition (veh)   
local sound_shoot = playSound3D(sound,x,y,z, false) 
setSoundMaxDistance( sound_shoot, 400 ) 
setSoundVolume(sound_shoot, 1) 
attachElements ( sound_shoot, veh, 0,0,0 ) 
  
local sound_reload = playSound3D("files/reload.wav",x,y,z, false) 
setSoundMaxDistance( sound_reload, 100 ) 
setSoundVolume(sound_reload, 1) 
attachElements ( sound_reload, veh, 0,0,0 ) 
end 
addEvent( "projectile_sound", true ) 
addEventHandler( "projectile_sound", localPlayer, projectile_sound ) 

Link to comment

client

sounds = {"files/fire1.wav", "files/fire2.wav", "files/fire3.wav"} 
sound = sounds [ math.random ( #sounds ) ] 
  
fired = false 
  
function projectile_sound(veh)           
local x,y,z = getElementPosition (veh)   
local sound_shoot = playSound3D(sound,x,y,z, false) 
setSoundMaxDistance( sound_shoot, 400 ) 
setSoundVolume(sound_shoot, 1) 
attachElements ( sound_shoot, veh, 0,0,0 ) 
  
setTimer ( function()     
local sound_reload = playSound3D("files/reload.wav",x,y,z, false) 
setSoundMaxDistance( sound_reload, 100 ) 
setSoundVolume(sound_reload, 1) 
attachElements ( sound_reload, veh, 0,0,0 ) 
end, 1000, 1 ) 
end 
addEvent( "projectile_sound", true ) 
addEventHandler( "projectile_sound", localPlayer, projectile_sound ) 
  
function projectile_client() 
if fired == false then 
    local veh = getPedOccupiedVehicle(localPlayer) 
        if getElementModel(veh) == 457 then 
        triggerServerEvent ( "projectile_server", resourceRoot, veh )  
        fired = true 
        setTimer ( function() 
        fired = false 
        end, 5000, 1 )          
        end 
    end 
end 
bindKey("mouse1", "down", projectile_client) 

server

  
  
function projectile_server(veh) 
triggerClientEvent ("projectile_sound", getRootElement(), veh) 
end 
addEvent( "projectile_server", true ) 
addEventHandler( "projectile_server", getRootElement(), projectile_server ) 
  
  
  

Link to comment

So you want to trigger the function from client to server and from server back to client, Lol.

Remove the server side code and simply use this:

fired = false 
  
function projectile_sound(veh)          
    local x,y,z = getElementPosition (veh)  
    local sound_shoot = playSound3D("files/fire"..math.random(1 ,3)..".wav", x, y, z, false) 
        setSoundMaxDistance( sound_shoot, 400 ) 
        setSoundVolume(sound_shoot, 1) 
        attachElements ( sound_shoot, veh, 0,0,0 ) 
  
    setTimer ( function()    
        local sound_reload = playSound3D("files/reload.wav",x,y,z, false) 
            setSoundMaxDistance( sound_reload, 100 ) 
            setSoundVolume(sound_reload, 1) 
            attachElements ( sound_reload, veh, 0,0,0 ) 
    end, 1000, 1 ) 
end 
  
function fire() 
if fired == false then 
    local vehicle = getPedOccupiedVehicle(localPlayer) 
        if getElementModel(vehicle) == 457 then 
        projectile_sound(vehicle) 
        fired = true 
        setTimer ( function() 
            fired = false 
            end, 5000, 1 )          
        end 
    end 
end 
bindKey("mouse1", "down", fire) 

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...