Montis. Posted March 6, 2012 Share Posted March 6, 2012 Hi. I have this script how can I add more songs? function startMusic() setRadioChannel(0) song = playSound("music2.mp3",true) outputChatBox("#ffffff* #C0FF3ETurn on/off Music Using #FFFFFF\"M\"",255,255,255,true) end function makeRadioStayOff() setRadioChannel(0) cancelEvent() end function toggleSong() if not songOff then setSoundVolume(song,0) songOff = true removeEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) else setSoundVolume(song,1) songOff = false setRadioChannel(0) addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) end end addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()),startMusic) addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) addEventHandler("onClientPlayerVehicleEnter",getRootElement(),makeRadioStayOff) addCommandHandler("musicmusic",toggleSong) bindKey("m","down","musicmusic") addEventHandler("onClientResourceStop",getResourceRootElement(getThisResource()),startMusic) Link to comment
Paper Posted March 6, 2012 Share Posted March 6, 2012 You have to add the mp3 file in the meta.xml and add a command like /playsong [1/2/3] where 1-2-3 are the song's id Link to comment
Montis. Posted March 6, 2012 Author Share Posted March 6, 2012 You have to add the mp3 file in the meta.xml and add a command like /playsong [1/2/3] where 1-2-3 are the song's id First thing i'm not very good in lua scripting(newbie). You have to add the mp3 file in the meta.xml I know it. But I just don't know how to create it in script. Link to comment
Paper Posted March 6, 2012 Share Posted March 6, 2012 local sondID function startMusic() setRadioChannel(0) song = playSound("music"..songID..".mp3",true) outputChatBox("#ffffff* #C0FF3ETurn on/off Music Using #FFFFFF\"M\"",255,255,255,true) end function makeRadioStayOff() setRadioChannel(0) cancelEvent() end function toggleSong(player, command, songID) if not songID and type(songID) == "number" then if not songOff then setSoundVolume(song,0) sondID = nil songOff = true removeEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) else setSoundVolume(song,1) songOff = false sondID = songID setRadioChannel(0) addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) end else outputChatBox("#ffffff* #C0FF3ECommand syntax: /musicmusic [songID]",255,255,255,true) end end addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()),startMusic) addEventHandler("onClientPlayerRadioSwitch",getRootElement(),makeRadioStayOff) addEventHandler("onClientPlayerVehicleEnter",getRootElement(),makeRadioStayOff) addCommandHandler("musicmusic",toggleSong) addEventHandler("onClientResourceStop",getResourceRootElement(getThisResource()),startMusic) Try this, but you can't use the bind key anymore Link to comment
drk Posted March 6, 2012 Share Posted March 6, 2012 Paper, your code is wrong local current local songID = { [ 1 ] = 'Your sound url / directory', [ 2 ] = 'Your sound url / directory', [ 3 ] = 'Your sound url / directory' } addEventHandler ( 'onClientResourceStart', resourceRoot, function ( ) local sound = songID [ math.random ( #songID ) ] current = playSound ( sound, false ) setRadioChannel ( 0 ) end ) addEventHandler ( 'onClientResourceStop', resourceRoot, function ( ) if isElement ( current ) then destroyElement ( current ) end end ) function radioOff ( ) setRadioChannel ( 0 ) cancelEvent ( ) end addEventHandler ( 'onClientPlayerRadioSwitch', root, radioOff ) addEventHandler ( 'onClientPlayerVehicleEnter', root, radioOff ) addCommandHandler ( 'sound', function ( thePlayer, command, id ) if ( type ( id ) ~= 'number' ) then return end if isElement ( current ) then destroyElement ( current ) end for i, music in ipairs ( songID ) do playSound ( i[id], false ) end end ) Not tested. Command /sound without < >. Link to comment
Castillo Posted March 6, 2012 Share Posted March 6, 2012 @Draken: Your code is wrong . local current local songID = { [ 1 ] = 'Your sound url / directory', [ 2 ] = 'Your sound url / directory', [ 3 ] = 'Your sound url / directory' } addEventHandler ( 'onClientResourceStart', resourceRoot, function ( ) local sound = songID [ math.random ( #songID ) ] current = playSound ( sound, false ) setRadioChannel ( 0 ) end ) function radioOff ( ) setRadioChannel ( 0 ) cancelEvent ( ) end addEventHandler ( 'onClientPlayerRadioSwitch', root, radioOff ) addEventHandler ( 'onClientPlayerVehicleEnter', root, radioOff ) addCommandHandler ( 'sound', function ( command, id ) local id = tonumber(id) if isElement ( current ) then destroyElement ( current ) end if ( songID[id] ) then current = playSound ( songID[id], false ) end end ) Link to comment
Kenix Posted March 6, 2012 Share Posted March 6, 2012 (edited) addCommandHandler ( 'sound', function ( thePlayer, command, id ) -- thePlayer only in server side addCommandHandler. if ( type ( id ) ~= 'number' ) then return end -- all arguments in addCommandHandler ( in client side ) is string not NUMBER.So it's condition fail. if isElement ( current ) then destroyElement ( current ) end for i, music in ipairs ( songID ) do playSound ( i[id], false ) -- i variable in loop is index's in table songID. I not understand what you want create here. end end ) Draken,Read comments. For all. local songID = { 'Your sound url / directory'; 'Your sound url / directory'; 'Your sound url / directory'; } And local songID = { [ 1 ] = 'Your sound url / directory'; [ 2 ] = 'Your sound url / directory'; [ 3 ] = 'Your sound url / directory'; } It's same codes, because indexes defined by default. for i,v in pairs { '1' } do print( i,v ) --> 1 1 end for i,v in pairs { [ 1 ] = '1' } do print( i,v ) --> 1 1 end Example Edited March 6, 2012 by Guest Link to comment
drk Posted March 6, 2012 Share Posted March 6, 2012 (edited) addCommandHandler ( 'sound', function ( thePlayer, command, id ) -- thePlayer only in server side addCommandHandler. if ( type ( id ) ~= 'number' ) then return end -- all arguments in addCommandHandler ( in client side ) is string not NUMBER.So it's condition fail. if isElement ( current ) then destroyElement ( current ) end for i, music in ipairs ( songID ) do playSound ( i[id], false ) -- i variable in loop is index's in table songID. I not understand what you want create here. end end ) Draken,Read comments. For all. local songID = { 'Your sound url / directory'; 'Your sound url / directory'; 'Your sound url / directory'; } And local songID = { [ 1 ] = 'Your sound url / directory'; [ 2 ] = 'Your sound url / directory'; [ 3 ] = 'Your sound url / directory'; } It's same codes, because index define by default. Thanks for explain I need to start learning Lua again LOL Edited March 6, 2012 by Guest Link to comment
Kenix Posted March 6, 2012 Share Posted March 6, 2012 No problem. Edit: for i,v in pairs { [ 1 ] = '1' } do -- It's not syntax error for i,v in pairs { [ 1 ] = '1' } do ,it's same for i,v in pairs ( { [ 1 ] = '1' } ) do print( i,v ) --> 1 1 end Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now