Forum

> > CS2D > Scripts > How to make random color in message ?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to make random color in message ?

10 replies
To the start Previous 1 Next To the start

old Re: How to make random color in message ?

mafia_man
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
math.randomseed(os.time())
function rndmsg(text)
	msg("©" .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. text)
end

function fix_part(s)
	s = tostring(s)
	
	local fix = 3 - string.len(s)
	if fix == 1 then return "0" .. s end
	if fix == 2 then return "00" .. s end
	return s
end

old Re: How to make random color in message ?

Rainoth
Moderator Off Offline

Quote
user mafia_man has written
1
2
3
4
5
6
7
8
9
10
11
12
13
math.randomseed(os.time())
function rndmsg(text)
	msg("©" .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. text)
end

function fix_part(s)
	s = tostring(s)
	
	local fix = 3 - string.len(s)
	if fix == 1 then return "0" .. s end
	if fix == 2 then return "00" .. s end
	return s
end


To make different messages I can make something like this ?

1
2
3
4
5
6
7
local randques = math.random(1,25)
if randques == 1 then
text = "text text text"
elseif randques == 2 then
text = "text bla text"
elseif randques == 3 then
text = "random text bla bla"

is that correct ? It's hard to understand your code for me (especially what's rndmsg/seed/os.time()

I was going to make it so when player presses button he gets a random question in random color.. but with your code that I don't understand I don't know how to change it so it would be different questions ..

EDIT : what about math.random(000,255) ?

old Re: How to make random color in message ?

Alistaire
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
randomQuestion = {
	'Are we strangers to love?',
	'Do you know the rules?',
	'Are you thinking of a full commitment?',
	'Would you get this from any other guy?',
	'Do you want to tell how you're feeling?',
	'Do you understand?',
	'Are you gonna give me up?',
	'Are you gonna let me down?',
	'Are you gonna run around and desert me?',
	'Are you gonna make me cry?',
	'Are you gonna say goodbye?',
	'Are you gonna tell a lie and hurt me?'
}

local question = randomQuestion[math.random(1, #randomQuestion)]

old Re: How to make random color in message ?

EP
User Off Offline

Quote
@user Rainoth: Yes. Random message is possible as well.

1
2
3
4
question = {"sup bro","wazzap?","how are you?","u liek pie?"}
function rndq()
	return question[math.random(1,#question)]
end
Add your questions into question table (respecting commas and strings).

And use it with user mafia_man's code so:

1
rndmsg(rndq)
It will call rndq function first and that function will return the question. Rndmsg will format your question with a color.. profit.

old Re: How to make random color in message ?

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
addhook("usebutton","school")
function school(id,x,y)
	if (x==40 and y==42) then
		math.randomseed(os.time())
		function rndmsg(rndq)
     			msg("©" .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. text)
		end
	end
end

function fix_part(s)
     s = tostring(s)
     
     local fix = 3 - string.len(s)
     if fix == 1 then return "0" .. s end
     if fix == 2 then return "00" .. s end
     return s
end

question = {"Why is the sky blue ?","Who found USA ?","Josh's father has 4 children. Jack John Jill. What's 4th childs name ?","What are the colors of the rainbow ?","If you shoot 1 out of 10 birds. How many are left ?","You have 2 cakes. I give you 60 and then you eat all of them. What do you have ?","Green ray of light passes a glass of wine and lands on wall. What's color of ray on wall ?","sin 90 = ?","2+2x2x3x3x2 = ?","Who found North America ?","What's the capital of Spain/Italy/France/Lithuania/Poland/Russia/Japan/China/USA ?","Which came first Chicken or the Egg ?"}
function rndq()
     return question[math.random(1,#question)]
end

Why does this not work ? It gives no error but it doesn't show message either. I made button in 40,42 (x,y) but when I press it, nothing happens..

old Re: How to make random color in message ?

mafia_man
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
math.randomseed(os.time())
function fix_part(s)
	s = tostring(s)
	local fix = 3 - string.len(s)
	if fix == 1 then return "0" .. s end
	if fix == 2 then return "00" .. s end
	return s
end

function rndmsg(text)
	msg("©" .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. text)
end

question = {"Why is the sky blue ?","Who found USA ?","Josh's father has 4 children. Jack John Jill. What's 4th childs name ?","What are the colors of the rainbow ?","If you shoot 1 out of 10 birds. How many are left ?","You have 2 cakes. I give you 60 and then you eat all of them. What do you have ?","Green ray of light passes a glass of wine and lands on wall. What's color of ray on wall ?","sin 90 = ?","2+2x2x3x3x2 = ?","Who found North America ?","What's the capital of Spain/Italy/France/Lithuania/Poland/Russia/Japan/China/USA ?","Which came first Chicken or the Egg ?"}
function rndq()
	return question[math.random(1, #question)]
end

addhook("usebutton","school")
function school(id,x,y)
	if x == 40 and y == 42 then
		rndmsg(rndq())
	end
end

old Re: How to make random color in message ?

Rainoth
Moderator Off Offline

Quote
user mafia_man has written
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
math.randomseed(os.time())
function fix_part(s)
	s = tostring(s)
	local fix = 3 - string.len(s)
	if fix == 1 then return "0" .. s end
	if fix == 2 then return "00" .. s end
	return s
end

function rndmsg(text)
	msg("©" .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. fix_part(math.random(0,255)) .. text)
end

question = {"Why is the sky blue ?","Who found USA ?","Josh's father has 4 children. Jack John Jill. What's 4th childs name ?","What are the colors of the rainbow ?","If you shoot 1 out of 10 birds. How many are left ?","You have 2 cakes. I give you 60 and then you eat all of them. What do you have ?","Green ray of light passes a glass of wine and lands on wall. What's color of ray on wall ?","sin 90 = ?","2+2x2x3x3x2 = ?","Who found North America ?","What's the capital of Spain/Italy/France/Lithuania/Poland/Russia/Japan/China/USA ?","Which came first Chicken or the Egg ?"}
function rndq()
	return question[math.random(1, #question)]
end

addhook("usebutton","school")
function school(id,x,y)
	if x == 40 and y == 42 then
		rndmsg(rndq())
	end
end


Works like a charm. Thanks !
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview