Jump to content

Sound not stopping, same sound could be heared in the same time, with a little delay.


Recommended Posts

Hi!
I'm making as vehicle-realism system. And when i exit the vehicle with no seatbelt on, and re enter it, the beeping alarm of the seatbelt could be heared multiple times, while it should be heared once per a second.

Clientside mains:

function beltWarning()
	stopSound (beltAlarm)
	beltAlarm = playSound ("alarm.mp3", false)
end
addEvent ("beltWarning", true)
addEventHandler ("beltWarning", getRootElement(), beltWarning)

function belt()
	seatBeltOn = playSound ("belt.wav", false)
end
addEvent ("belt", true)
addEventHandler ("belt", getRootElement(), belt)

function unbelt()
	seatBeltOff = playSound ("unbelt.wav", false)
end
addEvent ("unbelt", true)
addEventHandler ("unbelt", getRootElement(), unbelt)


Serverside mains: (there might be some useless things, it is not finished jet, i did not take out the ones i will use in action.)

function ovElmentDataAdas(jatekos)
	setElementData (jatekos, "bekapcsoltOv", false)
end
addEvent ("ovElmentDataAdas", true)
addEventHandler ("ovElmentDataAdas", getRootElement(), ovElmentDataAdas)
addEventHandler ("onVehicleEnter", getRootElement(), ovElmentDataAdas)

function ovElementDataElvetel(jatekos)
	if getElementData (jatekos, "bekapcsoltOv") == true then
	setElementData (jatekos, "biztonsagiOv", false)
	exports.cl_core:localMeAction(jatekos, "kicsatolta a biztonsági övét!")
	triggerClientEvent ("unbelt", jatekos)
	end
end
addEvent ("ovElementDataElvetel", true)
addEventHandler ("ovElementDataElvetel", getRootElement(), ovElementDataElvetel)
addEventHandler ("onVehicleExit", getRootElement(), ovElementDataElvetel)

function seatBelt(jatekos)
	if isPedInVehicle (jatekos) == true then
		if getElementData (jatekos, "bekapcsoltOv") == false then
			setElementData (jatekos, "bekapcsoltOv", true)
			exports.cl_core:localMeAction(jatekos, "becsatolta a biztonsági övét!")
			triggerClientEvent ("belt", jatekos)
		else
			setElementData (jatekos, "bekapcsoltOv", false)
			exports.cl_core:localMeAction(jatekos, "kicsatolta a biztonsági övét!")
			triggerClientEvent ("unbelt", jatekos)
		end
	end
end
addCommandHandler ("öv", seatBelt)
addCommandHandler ("sb", seatBelt)
addCommandHandler ("belt", seatBelt)
addCommandHandler ("seatb", seatBelt)
addCommandHandler ("seatbelt", seatBelt)

function bekapcsoltOvEllenorzese(jatekos)
	beeping = setTimer ( function()
		if isPedInVehicle (jatekos) == true and getElementData (jatekos, "bekapcsoltOv") == false then
			triggerClientEvent ("beltWarning", jatekos)
		end
		bindKey (jatekos, "F5", "down", "öv")
	end, 1000, 0)
end
addEventHandler ("onVehicleEnter", getRootElement(), bekapcsoltOvEllenorzese)

 

Link to comment

Try declaring the variables (in which timers will be stored) outside the functions first, like this:

local beltAlarm, seatBeltOn, seatBeltOff 

function beltWarning()
	stopSound (beltAlarm)
	beltAlarm = playSound ("alarm.mp3", false)
end
addEvent ("beltWarning", true)
addEventHandler ("beltWarning", getRootElement(), beltWarning)

function belt()
	seatBeltOn = playSound ("belt.wav", false)
end
addEvent ("belt", true)
addEventHandler ("belt", getRootElement(), belt)

function unbelt()
	seatBeltOff = playSound ("unbelt.wav", false)
end
addEvent ("unbelt", true)
addEventHandler ("unbelt", getRootElement(), unbelt)

 

Link to comment

Honestly, I haven't looked through your code to see where the error could be, because I think you should try to clean it up first. You might not know this, but when you change the element data and it is synced (4th argument is sync true/false), it will trigger an event on the client side as well --> You don't need to use triggerClientEvent as well when using setElementData, because essentially you are doing the same thing twice. Try to code the following (saves performance and easier to maintain).

-> User buckles their seat belt, get the current element data of the vehicle, something like "numSeatbelts" which will be the number of players with their seat belts turned on

-> Since you changed the element data, you can use the event "onClientElementDataChange" and make sure the element data was changed for that vehicle and for the "numSeatbelts", now get the number of passengers in the vehicle and if it equals to the number of seat belts, turn off the sound, if it's less do the playSound, also make sure they are all sitting in the "source" element (vehicle). This will also play the sound for everyone in the vehicle or if you want, you can even attach the sound element and others will hear it in '3D'. Make sure you are using a table client-side to be able to identify which sound to turn off.

 

EDIT:

If you need to set the element data individually for players because you will use it in other scripts (eg. less health loss on collision), you can also save the player ID's on the vehicle's elementData, separated by a character, so you can split it and see who has their set belt on (assuming you already have an ID system)

setElementData(veh, "seatBelts", "3,5,7") -> players with ID 3,5,7 have their set belts on, you can use split(string, ',') to get a table = {3,5,7}

Edited by pa3ck
Link to comment
  • Moderators

Your main problem is with the timer. Every time you enter the vehicle, it will create a new timer.

 


 

 

function bekapcsoltOvEllenorzese(jatekos)
    beeping = setTimer ( function()
        if isPedInVehicle (jatekos) == true and getElementData (jatekos, "bekapcsoltOv") == false then
            triggerClientEvent ("beltWarning", jatekos)
        end
        bindKey (jatekos, "F5", "down", "öv")
    end, 1000, 0)

end

█ Creation of an infinity timer and saved in a global variable which is never killed. (and created a new one every time you enter a vehicle, which is causing the double bleep effect)

 


 

But because you didn't debug your code properly you wouldn't be able to know that, as you are probably new to LUA.

So follow this tutorial:

IIYAMA: The understand of what your code is doing is more important than having working code.

Edited by IIYAMA
  • Like 1
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...