Jump to content

[HELP] About how to stop now playing sounds


28WL

Recommended Posts

Help me, please.

How to make:

A) If you click on the button, sound starts playing, you hear it.

B) If you click on the button again, that sound stops in directed time (for example in 10 seconds).

...buttons mark, unmark functions and other things (from login.lua DayZ).

function clickPanelButton(button, state) 
    if button == "left" and state == "up" then 
        local element = getElementData(getLocalPlayer(), "clickedButton") 
        if element then 
            local info = getElementData(element, "info") 
            if info and info == "login" then 
                do 
                    local username = guiGetText(Login_Edit[1]) 
                    local password = guiGetText(Login_Edit[2]) 
                    if tostring(username) ~= "" and tostring(password) ~= "" then 
                        triggerServerEvent("onClientSendLoginDataToServer", getLocalPlayer(), username, password) 
                    else 
                        reason = "Missing Password or Username." 
                        outputChatBox("[Login]:#FF9900 " .. reason, 255, 255, 255, true) 
                    end 
                end 
            elseif info and info == "intro" then 
                do 
                    playSound("Sounds/Intro.mp3") 
                end 
            elseif info and info == "guest" then 
                showLoginWindow(false) 
            elseif info and info == "register" then 
                local username = guiGetText(Login_Edit[3]) 
                local pass1 = guiGetText(Login_Edit[4]) 
                local pass2 = guiGetText(Login_Edit[5]) 
                if tostring(username) ~= "" then 
                    if tostring(pass1) ~= "" then 
                        if pass1 == pass2 then 
                            triggerServerEvent("onClientSendRegisterDataToServer", getLocalPlayer(), username, pass1) 
                        else 
                            reason = "Passwords not match." 
                            outputChatBox("[Register]:#FF9900 " .. reason, 255, 255, 255, true) 
                        end 
                    else 
                        reason = "No password was entered." 
                        outputChatBox("[Register]:#FF9900 " .. reason, 255, 255, 255, true) 
                    end 
                else 
                    reason = "No username was entered." 
                    outputChatBox("[Register]:#FF9900 " .. reason, 255, 255, 255, true) 
                end 
            end 
        end 
    end 
end 
addEventHandler("onClientClick", getRootElement(), clickPanelButton) 

...other scripts, functions.

Link to comment

You can use setTimer to delay the stop. You can use variables to define whether the timer is running and if the sound is playing, this way you won't get issues with multiple sounds and multiple timers and such.

You can use the code below.

local secondsToWait = 10 
local soundElement, soundTimer 
  
function clickPanelButton(button, state) 
    if (button == "left") and (state == "up") then 
        local element = getElementData(localPlayer, "clickedButton") 
        if (element) then 
            local info = getElementData(element, "info") 
            if (info) then 
                if (info == "login") then 
                    local username = guiGetText(Login_Edit[1]) 
                    local password = guiGetText(Login_Edit[2]) 
                     
                    if (tostring(username) ~= "") and (tostring(password) ~= "") then 
                        triggerServerEvent("onClientSendLoginDataToServer", localPlayer, username, password) 
                    else 
                        reason = "Missing password or username." 
                        outputChatBox("[Login]:#FF9900 " .. reason, 255, 255, 255, true) 
                    end 
                elseif (info == "intro") then 
                    if (not soundElement) then -- If there is no sound playing yet 
                        soundElement = playSound("Sounds/Intro.mp3") -- Define and play the sound 
                    else 
                        if (not soundTimer) then -- If there is no timer yet 
                            soundTimer = setTimer(function() -- Create a new timer 
                                if (isElement(soundElement)) then -- Check if the sound exists 
                                    destroyElement(soundElement) -- Destroy the sound 
                                    soundElement, soundTimer = nil -- Reset the variables 
                                end 
                            end, secondsToWait*1000, 1) -- Wait for the amount of seconds defined in 'secondsToWait' 
                        end 
                    end 
                elseif (info == "guest") then 
                    showLoginWindow(false) 
                elseif (info == "register") then 
                    local username = guiGetText(Login_Edit[3]) 
                    local pass1 = guiGetText(Login_Edit[4]) 
                    local pass2 = guiGetText(Login_Edit[5]) 
                     
                    if (tostring(username) ~= "") then 
                        if (tostring(pass1) ~= "") then 
                            if (pass1 == pass2) then 
                                triggerServerEvent("onClientSendRegisterDataToServer", localPlayer, username, pass1) 
                            else 
                                reason = "Passwords don't match." 
                                outputChatBox("[Register]:#FF9900 " .. reason, 255, 255, 255, true) 
                            end 
                        else 
                            reason = "No password was entered." 
                            outputChatBox("[Register]:#FF9900 " .. reason, 255, 255, 255, true) 
                        end 
                    else 
                        reason = "No username was entered." 
                        outputChatBox("[Register]:#FF9900 " .. reason, 255, 255, 255, true) 
                    end 
                end 
            end 
        end 
    end 
end 
addEventHandler("onClientClick", root, clickPanelButton) 

By the way, you had some unnecessary do statements there, you don't need those, I doubt it would even work since you need some form of loop to even use do statement in the first place.

Link to comment
  • 2 weeks later...

Hmmm...

I was thinking about that, but no.

Well, now I don't need that help, because +1 more button at the login panel fucks all beautiful view.

How it should look like: decreasing sound's voice in 5 seconds and then stopping it 100%?

Server:

addCommandHandler ( "playsound1", 
    function ( play_sound_1 ) 
        if isAccountInGroup ( { "Super Admin", "Admin" }, getAccountName ( getPlayerAccount ( play_sound_1 ) ) ) then 
            setTimer ( triggerClientEvent, 1000, 1, "play_sound_1", root, "sounds/sound_1.mp3" ) 
        else 
            outputChatBox ("` You have no acces to that command.", play_sound_1 ) 
        end 
    end 
) 
  
function isAccountInGroup ( groups, account ) 
    local theGroup = false 
    for _, group in ipairs ( groups ) do 
        if isObjectInACLGroup ( "user.".. account, aclGetGroup ( group ) ) then 
            theGroup = group 
            break 
        end 
    end 
    return theGroup 
end 
  

Client:

addEvent ( "play_sound_1", true ) 
addEventHandler ( "play_sound_1", root, 
    function ( path1 ) 
        playSound ( path1 ) 
    end 
) 

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