Forum

> > CS2D > Scripts > Playing sounds randomly on joining/leaving
Forums overviewCS2D overview Scripts overviewLog in to reply

English Playing sounds randomly on joining/leaving

5 replies
To the start Previous 1 Next To the start

old Playing sounds randomly on joining/leaving

GeoB99
Moderator Off Offline

Quote
Hey there,

After practising a little at making some simple scripts until I'll get used with the basics (at least), I was thinking about if there is a way or how to make it possible to play/start a random sound while the specific player is joining the server or leaving it. To make it more clear, here's the code below:
1
2
3
4
5
6
7
8
9
10
11
addhook ("join","joinMessage")
function joinMessage(id)
	msg(string.char(169).."124250000The player named "..player(id,"name").." has joined the server!")
	parse("sv_sound \"Sounds/misc/himan.wav\"")
end

addhook ("leave","Leaving")
function Leaving(id)
	msg(player(id,"name").." "..string.char(169).."000255255has left the server! Hope we'll see him again soon.")
	parse("sv_sound \"Sounds/misc/gtg.wav\"")
end
I know right, there's nothing impressive nor special in this code. Basically, this script contains only cs2d lua hook join and cs2d lua hook leave hooks but that's the beginning. What I want to do is when a player joins the server, it will choose a sound randomly, same for when a player leaves the server.

Someone told me, you must use math.random for that, although I dunno where or how I put this. It'd be appreciated if someone can give me an example.

old Re: Playing sounds randomly on joining/leaving

Mami Tomoe
User Playing CS2D

Quote
1
2
join.random.sound = math.random("1,10")
leave.random.sound = math.random("1,10")

will select a sound between 1 to 10 (note u have to declare leave.random.sound and join.random.sound)

to use it do:

1
2
3
4
5
if join.random.sound == 1 then
parse("sv_sound SOUND HERE")
elseif join.random.sound == 2 then
... you know the deal here right?
end

same for leave

Edit:
Finished code (note NOT tested)
Spoiler >
edited 1×, last 12.11.15 07:42:57 pm

old Re: Playing sounds randomly on joining/leaving

Nekomata
User Off Offline

Quote
not tested. should work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
join_sfx_path = {
	[1] = "your/sound/path/here.ogg",
	[2] = "another/one/here.mp3",
	[3] = "keep/on/adding/em.ogg",
}
leave_sfx_path = {
	[1] = "your/sound/path/here.ogg",
	[2] = "another/one/here.mp3",
	[3] = "keep/on/adding/em.ogg",
}

addhook ("join","joinMessage")
function joinMessage(id)
   	msg(string.char(169).."124250000The player named "..player(id,"name").." has joined the server!")
   	local rand_sfx = math.random(1,#join_sfx_path)
   	parse("sv_sound \""..join_sfx_path[rand_sfx].."\"")
end

addhook ("leave","Leaving")
function Leaving(id)
   	msg(player(id,"name").." "..string.char(169).."000255255has left the server! Hope we'll see him again soon.")
   	local rand_sfx = math.random(1,#leave_sfx_path)
   	parse("sv_sound \""..leave_sfx_path[rand_sfx].."\"")
end

Elaborated >
edited 1×, last 12.11.15 08:38:58 pm

old Re: Playing sounds randomly on joining/leaving

Rainoth
Moderator Off Offline

Quote
name your files like this
gtg1, gtg2, etc.
himan1, himan2 etc.
then do
local randomSong = math.random(1,2)
or whatever amount of songs u want to have
then call it
parse("sv_sound \"Sounds/misc/himan"..randomSong..".wav\"")

Least writing. Least hasle. 9/10 I approve.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview