Jump to content

[HELP] Stop/Play music when you select checkbox


..:D&G:..

Recommended Posts

Hello, I am trying to make something but ehm... I made a function which stops a song when a checkbox (songcheckbox) is checked, and play it when it's not.

function checkboxSong() 
    if ( getElementType(songcheckbox) == "gui-checkbox" ) then 
        if (guiCheckBoxGetSelected(songcheckbox)) then 
            stopSound(song) 
        else 
            playSound(song) 
        end 
    end 
end 
addEventHandler("onClientGUIClick", root, checkboxSong, false) 

The song line is in a function before this one, and it looks something like this:

song = playSound("http://example.com/song.mp3") 

Now, I don't get why it isn't working :( Can someone please tell me what's wrong here?

Thank you.

Link to comment
function checkboxSong() 
    if(source == songcheckbox) then 
        if(guiCheckBoxGetSelected(songcheckbox)) then 
            stopSound(song) 
        else 
            playSound(song) 
        end 
    end 
end 
addEventHandler("onClientGUIClick", root, checkboxSong) 

Link to comment
function checkboxSong() 
    if(source == songcheckbox) then 
        if(guiCheckBoxGetSelected(songcheckbox)) then 
            stopSound(song) 
        else 
            playSound(song) 
        end 
    end 
end 
addEventHandler("onClientGUIClick", root, checkboxSong) 

Thanks, that worked, but only for stopSound. After I unchecked the box, it doesn't play the sound again and I get the following error:

Bad argument @ 'playSound' [Expected string at argument 1]

Link to comment
function checkboxSong() 
    if(source == songcheckbox) then 
        if(guiCheckBoxGetSelected(songcheckbox)) then 
            stopSound(song) 
        elseif not (guiCheckBoxGetSelected(songcheckbox)) then 
            playSound(song) 
        end 
    end 
end 
addEventHandler("onClientGUIClick", root, checkboxSong) 

but?

song = playSound("http://example.com/song.mp3") 

how is that going to work if you already wrote playSound inside your function?

it should be

song = "http://example.com/song.mp3" 

Link to comment
function checkboxSong() 
    if(source == songcheckbox) then 
        if(guiCheckBoxGetSelected(songcheckbox)) then 
            stopSound(song) 
        elseif not (guiCheckBoxGetSelected(songcheckbox)) then 
            playSound(song) 
        end 
    end 
end 
addEventHandler("onClientGUIClick", root, checkboxSong) 

but?

song = playSound("http://example.com/song.mp3") 

how is that going to work if you already wrote playSound inside your function?

it should be

song = "http://example.com/song.mp3" 

The line song = playSound("http://example.com/song.mp3") is in another function, I am only triggering that line from another function.

Link to comment

[quote name=..:D&G:..]

function checkboxSong() 
    if(source == songcheckbox) then 
        if(guiCheckBoxGetSelected(songcheckbox)) then 
            stopSound(song) 
        elseif not (guiCheckBoxGetSelected(songcheckbox)) then 
            playSound(song) 
        end 
    end 
end 
addEventHandler("onClientGUIClick", root, checkboxSong) 

but?

song = playSound("http://example.com/song.mp3") 

how is that going to work if you already wrote playSound inside your function?

it should be

song = "http://example.com/song.mp3" 

The line song = playSound("http://example.com/song.mp3") is in another function, I am only triggering that line from another function.

If you don't want the error "Bad argument @ 'playSound' [Expected string at argument 1]", then you should do it like he showed you.

If you want to keep "song", define something like songURL = "http://songurl.com/song.mp3" and use it for the stopSound and playSound functions.

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