Jump to content

Reproduce a sound when the door opens


Elmatus

Recommended Posts

Posted

I want tha a sound get reproduced when i close or open the gate, this is my code

function openMyGate () 
   moveObject ( myGate, 500, -781.20001220703, 2624.8000488281, 104.09999847412, 0, 0, 0 )       
end 
addCommandHandler("opengate",openMyGate)   

Posted

@Elmatus: If your script is server side, then what hassan posted won't work, since playSound is client side only. You would need to use triggerClientEvent to trigger a client side event for then play the sound.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

--Server Side

  
function openMyGate () 
   moveObject (myGate, 500, -781.20001220703, 2624.8000488281, 104.09999847412, 0, 0, 0) 
    triggerClientEvent ( "sound", getRootElement()) 
end 
addCommandHandler ("opengate", openMyGate) 

--Client Side

function sound() 
sound = playSound ("sounds/opengate.mp3") 
setSoundVolume (sound, 0.5) 
end 
addEvent( "sound", true ) 
addEventHandler( "sound", getRootElement(),sound ) 

Posted

--Server Side

  
function openMyGate (p) 
   moveObject (myGate, 500, -781.20001220703, 2624.8000488281, 104.09999847412, 0, 0, 0) 
    triggerClientEvent ( "onGateOpening", p) 
end 
addCommandHandler ("opengate", openMyGate) 

--Client Side

addEvent( "onGateOpening", true ) 
  
function sound() 
sound = playSound ("sounds/opengate.mp3") 
end 
  
addEventHandler( "onGateOpening", getRootElement(),sound ) 

Posted
--Server Side
  
function openMyGate (p) 
   moveObject (myGate, 500, -781.20001220703, 2624.8000488281, 104.09999847412, 0, 0, 0) 
    triggerClientEvent ( "onGateOpening", p) 
end 
addCommandHandler ("opengate", openMyGate) 

--Client Side

addEvent( "onGateOpening", true ) 
  
function sound() 
sound = playSound ("sounds/opengate.mp3") 
end 
  
addEventHandler( "onGateOpening", getRootElement(),sound ) 

Small fixes on server side code:

addCommandHandler('opengate', 
    function(p) 
        if (moveObject(myGate, 500, -781.20001220703, -2624.8000488281, 104.09999847412, 0, 0, 0)) then 
            triggerClientEvent(p, 'onClientPlayerOpenGate', p) 
        end 
    end 
) 

And client side code:

addEvent('onClientPlayerOpenGate', true) 
  
addEventHandler('onClientPlayerOpenGate', root, 
    function() 
        playSound('sounds/opengate.mp3') 
    end 
) 

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

Posted
playSound3D ( string soundPath, float x, float y, float z, [ bool looped = false ] ) 

soundPath: the path for the sound you like , e.g. , "sound.mp3".

float x , float y, float z : the position you like to run the sound in.

looped: A boolean representing whether the sound will be looped. To loop the sound, use true.

setSoundMaxDistance ( element sound, int distance ) 

sound: a sound element.

distance: the default value for this is 10.

F4MZM4.gif

Posted

Like this:

playSound3D ( gate.mp3, -756.82775878906, 2842.2609863281, 55.485198974609, [ bool looped = false ] ) 
   setSoundMaxDistance ( gate.mp3, 10 ) 

????

And i have to edit the lua and add:

<script src="gate.mp3" type="server" /> 

??????

Posted
Like this:
playSound3D ( gate.mp3, -756.82775878906, 2842.2609863281, 55.485198974609, [ bool looped = false ] ) 
   setSoundMaxDistance ( gate.mp3, 10 ) 

????

And i have to edit the lua and add:

<script src="gate.mp3" type="server" /> 

??????

local sound = playSound3D ( "gate.mp3", -756.82775878906, 2842.2609863281, 55.485198974609, true ) 
setSoundMaxDistance ( sound, 10 ) 

This should be client side.

F4MZM4.gif

Posted

You can, use Anderl's code by replace the 'p' variable in the triggerClientEvent with 'getRootElement()'

"First they ignore you, then they laugh at you, then they fight you, then you win."

- Mahatma Gandhi (1869-1948)

Posted
But i want that when i use /opengate all the players near to me hear the sound, it works for that?

Yes , and here's the code

server

addCommandHandler('opengate', 
function(p) 
    if (moveObject(myGate, 500, -781.20001220703, -2624.8000488281, 104.09999847412, 0, 0, 0)) then 
        triggerClientEvent(p, 'onClientPlayerOpenGate', p) 
    end 
end 
) 

client

addEvent('onClientPlayerOpenGate', true) 
addEventHandler('onClientPlayerOpenGate', root, 
function() 
    local sound = playSound3D ( "sounds/opengate.mp3", -756.82775878906, 2842.2609863281, 55.485198974609, true ) 
    setSoundMaxDistance ( sound, 10 ) 
end 
) 

F4MZM4.gif

Posted

I used:

function openMyGate () 
   moveObject ( myGate1, 14000, -758.40002441406, 2843.1999511719, 50, 0, 0, 0 )  
end   
addCommandHandler("opengate",openMyGate)   
function sound (p)  
    if (moveObject(myGate, 500, -781.20001220703, -2624.8000488281, 104.09999847412, 0, 0, 0)) then 
        triggerClientEvent(p, 'onClientPlayerOpenGate', p) 
    end 
end 
addEvent('onClientPlayerOpenGate', true) 

But the sound dont get reproduced

Posted

BTW, I don't see the element myGate, So i guess that you've got a object scripted in the serverside file.

--server 
function openMyGate () 
   moveObject ( myGate, 500, -781.20001220703, 2624.8000488281, 104.09999847412, 0, 0, 0 )   
  triggerClientEvent ( "playsound", getRootElement())     
end 
addCommandHandler("opengate",openMyGate)   
  

  
--client 
function sound() 
sound = playSound3D ("opengate.mp3", -781.20001220703, 2624.8000488281, 104.09999847412, false) 
setSoundMaxDistance ( sound, 10 ) 
end 
addEvent( "playsound", true ) 
addEventHandler( "playsound", getRootElement(),sound ) 

meta:

  
<script src="server.lua" type="server" /> 
<script src="client.lua" type="client" /> 
<file src="opengate.mp3" type="client" /> 

Sometimes I dream about cheese

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