Sign in to follow this  
Montis.

Need help.

Recommended Posts

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) 

Share this post


Link to post

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 :D

Share this post


Link to post
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 :D

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.

Share this post


Link to post
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

Share this post


Link to post

Paper, your code is wrong :x

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

Share this post


Link to post

@Draken: Your code is wrong :P.

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 
) 

Share this post


Link to post
  
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 by Guest

Share this post


Link to post
  
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 :):S

I need to start learning Lua again LOL :cry:

Edited by Guest

Share this post


Link to post

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 
  

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.