How to check if the server is playing a sound
30 replies21.01.17 05:44:59 pm
Hi I want to make a script that plays sounds but I don't want the sounds to play all together I want it to play by order and the sounds will be chosen randomly so I can't use timers plz help thx bye

It's hard being the best girl in the whole entire world
You must use time, second/ms100 hook or anything about time. Do a true parameter whenever sound starts. Save its second duration at another parameter. Then do something like when that duration parameter bigger than 0, reduce it by 1 at second hook. When duration parameter reached to 0, then false the first parameter I said.
You can do it even using only duration parameter.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
par1 = false
par2 = 0
function sound_(path)
if par1 then return end
parse("sv_sound ...")
par1 = true
par2 = SOUND'S SECOND DURATION
end
addhook("second","sec")
function sec()
if par2 > 0 then
par2 = par2 - 1
else
par1 = false
end
end
par2 = 0
function sound_(path)
if par1 then return end
parse("sv_sound ...")
par1 = true
par2 = SOUND'S SECOND DURATION
end
addhook("second","sec")
function sec()
if par2 > 0 then
par2 = par2 - 1
else
par1 = false
end
end
You can do it even using only duration parameter.
Shit your pants:
Outlast II Mod (29) | Create your UI faster: CS2D UI Framework


@
Mami Tomoe: How will the sounds get triggered? By pressing a button or by saying a specific text?

I don't know what I'm doing with my life.
By killing and @
Masea: your method is too hard I have like 20 or more sounds and they are all triggered randomly

It's hard being the best girl in the whole entire world
Use tables then.
Trust me, this is not hard...
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
SOUNDS = {
[1] = {
path = "wada.wav",
duration = 132, --sec
},
[2] = {
path = "asdasdasd.wav",
duration = 92, --sec
}
}
local randomsound = math.random(1,#SOUNDS)
parse('sv_sound '..SOUNDS[randomsound].path)
[1] = {
path = "wada.wav",
duration = 132, --sec
},
[2] = {
path = "asdasdasd.wav",
duration = 92, --sec
}
}
local randomsound = math.random(1,#SOUNDS)
parse('sv_sound '..SOUNDS[randomsound].path)
Trust me, this is not hard...
Shit your pants:
Outlast II Mod (29) | Create your UI faster: CS2D UI Framework


Yeah but each kill triggers a random sound and has a chance to trigger even more sounds like double kill + godlike and other players might kill as well and this will also show images on screen so I really need to check if it's playing a sound...
It's hard being the best girl in the whole entire world
@
Mami Tomoe: Try this code, I'm not sure if this is what you are demanding.
You can put more sound files in the table by providing its name, extension and path.

Code:
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
sounds = {
[1] = "sfx/NAME1.wav";
[2] = "sfx/NAME2.wav";
[3] = "sfx/NAME3.wav";
};
function _kill(id)
parse("sv_sound "..sounds[math.random(1, #sounds)]);
end
addhook("kill", "_kill")
[1] = "sfx/NAME1.wav";
[2] = "sfx/NAME2.wav";
[3] = "sfx/NAME3.wav";
};
function _kill(id)
parse("sv_sound "..sounds[math.random(1, #sounds)]);
end
addhook("kill", "_kill")
You can put more sound files in the table by providing its name, extension and path.
I don't know what I'm doing with my life.
Kill has a chance to play a sound or more
For example:
Double kill!
Legendary!
and other players might kill as well so I don't want the sounds to play all together so plz is there a way to just CHECK if a sound is being played?? plz @
DC: or anyyyy @
Omghelpme:
For example:
Double kill!
Legendary!
and other players might kill as well so I don't want the sounds to play all together so plz is there a way to just CHECK if a sound is being played?? plz @


It's hard being the best girl in the whole entire world
@
Mami Tomoe: What about if the sound plays only for the killer? Will that solve your issue?

I don't know what I'm doing with my life.
@
Mami Tomoe: Don't forget my first post dude. I did what you've wanted from us already.

Shit your pants:
Outlast II Mod (29) | Create your UI faster: CS2D UI Framework


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
--untested
-- edit it.
sounds = {"mysound1", "mysound2"}
times = {3000, 5000} -- first song is 3 secs long, 2nd is 5secs long
isSoundRunning = false
function killSound()
isSoundRunning = false
end
function playSound(id)
if (isSoundRunning == false) then
parse("sound "..sounds[id])
timer(times[id], "killSound")
isSoundRunning = true
end
end
-- edit it.
sounds = {"mysound1", "mysound2"}
times = {3000, 5000} -- first song is 3 secs long, 2nd is 5secs long
isSoundRunning = false
function killSound()
isSoundRunning = false
end
function playSound(id)
if (isSoundRunning == false) then
parse("sound "..sounds[id])
timer(times[id], "killSound")
isSoundRunning = true
end
end

Do you want to hear the ignored sounds after the current sound has ended?
(Example: while "doublekill" is playing, someone did a tripple kill so after "doublekill" has ended, youll hear a "tripplekill")
autor has written:
so plz is there a way to just CHECK if a sound is being played?
Yes, if you keep track of the time. You can make a function like getCurrentSound().
Share time limited free games here

I edited @
Bowlinghead:'s code so it uses a queue:
The kill hook is just an example. You call the queueSound function whenever you need to play a sound, and then the ms100 hook will take care of playing the sounds in order.
PS: not tested.
PS 2: Queueing many sounds in short intervals will cause a total mess. So be careful.

The kill hook is just an example. You call the queueSound function whenever you need to play a sound, and then the ms100 hook will take care of playing the sounds in order.
PS: not tested.
PS 2: Queueing many sounds in short intervals will cause a total mess. So be careful.
@
Zeik: Thank you! It seems like it might work
Hello!!! < This is an edit!
I added this function because I need it to play sometimes to one player only
But when I use it the game freeze ( stuck in while!
)
plz help thx


Hello!!! < This is an edit!

I added this function because I need it to play sometimes to one player only

But when I use it the game freeze ( stuck in while!

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
function playSound2(id, sfx)
local temp=false
while temp==false do
if (isSoundRunning == false) then
if (not List.isEmpty(queue)) then
isSoundRunning = true
parse("sv_sound2 "..id.." "..sfx[1])
timer(sfx[2], "killSound")
temp=true
end
end
end
end
local temp=false
while temp==false do
if (isSoundRunning == false) then
if (not List.isEmpty(queue)) then
isSoundRunning = true
parse("sv_sound2 "..id.." "..sfx[1])
timer(sfx[2], "killSound")
temp=true
end
end
end
end
plz help thx

edited 1×, last 23.01.17 05:14:32 pm
It's hard being the best girl in the whole entire world
@
Mami Tomoe: I don't know what you're trying to do but it won't work like that because the while loop will only end when isSoundRunning is false (is the only if block that changes temp state). So when it's true it's an infinite loop, thus the game freezes.

I need a function that will
sv_sound2
because the code only supports sv_sound
It's hard being the best girl in the whole entire world
I don't know how costly it is to have a private queue for each player but it's what you're needing.
Here's the complete code:
Again, it's not tested.
PS: Basically, 0 is for global sound, and the rest are private.
Here's the complete code:
Again, it's not tested.
PS: Basically, 0 is for global sound, and the rest are private.
edited 1×, last 23.01.17 09:31:04 pm
It does a lot of errors just spamming red
The previous code worked I just need a function that will send it to a single player
I need support for
plz

The previous code worked I just need a function that will send it to a single player
I need support for
sv_sound
and sv_sound2
plz
It's hard being the best girl in the whole entire world
@
Mami Tomoe: You should post the errors otherwise I can't help much...

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
LUA ERROR: sys/lua/cool stuff/play.lua:27: attempt to perform arithmetic on field 'first' (a nil value)
-> sys/lua/cool stuff/play.lua:27: in function 'push'
-> sys/lua/cool stuff/play.lua:61: in function 'queueSound'
-> sys/lua/cool stuff/main.lua:4: in main chunk
-> [C]: in function 'dofile'
-> sys/lua/server.lua:2: in main chunk
LUA ERROR: sys/lua/cool stuff/play.lua:38: attempt to compare two nil values
-> sys/lua/cool stuff/play.lua:38: in function 'isEmpty'
-> sys/lua/cool stuff/play.lua:66: in function 'playSound'
-> sys/lua/cool stuff/play.lua:82: in function <sys/lua/cool stuff/play.lua:81>
LUA ERROR: sys/lua/cool stuff/play.lua:38: attempt to compare two nil values
-> sys/lua/cool stuff/play.lua:38: in function 'isEmpty'
-> sys/lua/cool stuff/play.lua:66: in function 'playSound'
-> sys/lua/cool stuff/play.lua:82: in function <sys/lua/cool stuff/play.lua:81>
-> sys/lua/cool stuff/play.lua:27: in function 'push'
-> sys/lua/cool stuff/play.lua:61: in function 'queueSound'
-> sys/lua/cool stuff/main.lua:4: in main chunk
-> [C]: in function 'dofile'
-> sys/lua/server.lua:2: in main chunk
LUA ERROR: sys/lua/cool stuff/play.lua:38: attempt to compare two nil values
-> sys/lua/cool stuff/play.lua:38: in function 'isEmpty'
-> sys/lua/cool stuff/play.lua:66: in function 'playSound'
-> sys/lua/cool stuff/play.lua:82: in function <sys/lua/cool stuff/play.lua:81>
LUA ERROR: sys/lua/cool stuff/play.lua:38: attempt to compare two nil values
-> sys/lua/cool stuff/play.lua:38: in function 'isEmpty'
-> sys/lua/cool stuff/play.lua:66: in function 'playSound'
-> sys/lua/cool stuff/play.lua:82: in function <sys/lua/cool stuff/play.lua:81>
main is the main script that makes cool hooks
play is the script you gave me
score is the script that sends sound commands to play
It's hard being the best girl in the whole entire world
There is no Lua command to check if CS2D is still playing a sound. That's because Lua only runs on the server and the server does not render anything or play any sounds at all if it's a headless dedicated server.
In fact this functionality is completely removed from dedicated servers because it's not needed and it might even lead to issues because servers commonly don't have audio and video drivers at all. Therefore the server does not even know the length of the used audio files.
So the only thing you can do is to - like already suggested - save the duration of all audio samples somehow and check the duration manually.
To solve the issue with the per-player sound you could use
timer and just put the player ID in as parameter:
In fact this functionality is completely removed from dedicated servers because it's not needed and it might even lead to issues because servers commonly don't have audio and video drivers at all. Therefore the server does not even know the length of the used audio files.
So the only thing you can do is to - like already suggested - save the duration of all audio samples somehow and check the duration manually.
To solve the issue with the per-player sound you could use

Code:
This value can then be used in the function called by the timer to play the next sound for the player with that particular ID. 1
timer(soundDuration, "callbackFunction", playerID)