Jump to content

[HELP] Stop/Play music when you select checkbox


..:D&G:..

Recommended Posts

Posted

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.

76561198066470453.png

SP37Ecj.jpg?2MTA:Rust Pre-Alpha Build v.0.3:SP37Ecj.jpg?2

https://forum.mtasa.com/viewtopic.php?f=114&t=97848

2Pac: ''Only God can judge me!''

Posted
if getElementType(source) then 

any errors ?

#include <iostream>

int main() {

    std::cout << "C++ is amazing <3" << std::endl;

    return 0;

}

I left MTA !... I m doing some tuts about (servers and scripting here) subscribe please

Posted

Show us where song defined

#include <iostream>

int main() {

    std::cout << "C++ is amazing <3" << std::endl;

    return 0;

}

I left MTA !... I m doing some tuts about (servers and scripting here) subscribe please

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

If I help you in a thread and you need further assistance, please don't PM me - use the thread you created instead. This way everyone on the forum can take advantage of it.

Posted

Song should be url or file see wiki

#include <iostream>

int main() {

    std::cout << "C++ is amazing <3" << std::endl;

    return 0;

}

I left MTA !... I m doing some tuts about (servers and scripting here) subscribe please

Posted
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]

76561198066470453.png

SP37Ecj.jpg?2MTA:Rust Pre-Alpha Build v.0.3:SP37Ecj.jpg?2

https://forum.mtasa.com/viewtopic.php?f=114&t=97848

2Pac: ''Only God can judge me!''

Posted
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" 

560x95_FFFFFF_FF9900_000000_000000.png

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

76561198066470453.png

SP37Ecj.jpg?2MTA:Rust Pre-Alpha Build v.0.3:SP37Ecj.jpg?2

https://forum.mtasa.com/viewtopic.php?f=114&t=97848

2Pac: ''Only God can judge me!''

Posted

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

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