Forum

> > CS2D > Scripts > Upvalue of "color" (nil error)
Forums overviewCS2D overview Scripts overviewLog in to reply

English Upvalue of "color" (nil error)

9 replies
To the start Previous 1 Next To the start

old Upvalue of "color" (nil error)

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
24
25
26
27
28
29
30
local rnd1,rnd2,rnd3,color
addhook("minute","changecolor")
function changecolor()
rnd1 = tostring(math.random(0,255))
rnd2 = tostring(math.random(0,255))
rnd3 = tostring(math.random(0,255))
color = ("0"):rep(3-#rnd1)..rnd1..("0"):rep(3-#rnd2)..rnd2..("0"):rep(3-#rnd3)..rnd3
end

addhook("say","coloredtext")
function coloredtext(id,txt)
	for k,v in ipairs(admin) do
		if player(id,"usgn") == v then
			msg("©255255255(Admin)"..player(id,"name").." : © "..color.." "..txt)
			return 1
		end
	end
	for k,v in ipairs(mod) do
		if player(id,"usgn") == v then
			msg("©255255255(Moderator)"..player(id,"name").." : © "..color.." "..txt)
			return 1
		end
	end
	for k,v in ipairs(vip) do
		if player(id,"usgn") == v then
			msg("©255255255(VIP)"..player(id,"name").." : © "..color.." "..txt)
			return 1
		end
	end
end

Could anyone point out the mistake in here ? I get error
1
46:Attempt to concatenate upvalue 'color' (a nil value)

But I'm preety sure I concatenated everything fine. I don't need a working remake. Fix of this script or even better a simple answer telling what's wrong will suffice. Thanks in advance

old Re: Upvalue of "color" (nil error)

Infinite Rain
Reviewer Off Offline

Quote
you can do # on a numeric values.

do this instead
1
("0"):rep(3-#tostring(rnd1))..rnd1..("0"):rep(3-#tostring(rnd2))..rnd2..("0"):rep(3-#tostring(rnd3))..rnd3

old Re: Upvalue of "color" (nil error)

Alistaire
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
25
26
27
28
29
30
31
32
33
ranks = {
	[123] = 1,
	[456] = 2,
	[789] = 3
}

rankNames = {
	'(VIP)',
	'(Moderator)',
	'(Admin)'
}

function lengthenString(s, length)
	while string.len(s) < length do
		s = '0'..s
	end
	return s;
end

function generateRandRGB()
	function rgb()
		return lengthenString(tostring(math.random(0, 255)))
	end
	return '©'..rgb()..rgb()..rgb()
end

function sayHook(id, txt)
	local rank = ranks[usgn]
	if rank then
		msg('©255255255'..rankNames[rank]..player(id, 'name')..': '..generateRandRGB..txt)
		return 1
	end
end

My take on your code.

old Re: Upvalue of "color" (nil error)

Rainoth
Moderator Off Offline

Quote
user Rainoth has written
I don't need a working remake. Fix of this script or even better a simple answer telling what's wrong will suffice. Thanks in advance


Because I simply don't understand what others write. Sure it's probably the other way around too but I won't really know how to make something like this in the future if I don't make it myself. I just need an explanation or a plain guess of what might be wrong.. Thanks for your user Alistaire: function tho.

old Re: Upvalue of "color" (nil error)

Alistaire
User Off Offline

Quote
You care too much about randoms

- You're defining the variables, before even doing stuff with it. local rnd1, rnd2, rnd3 and color are useless.

- You're using rnd1, rnd2, rnd3 as seperate and individual, useful string. They are not.

What you should use:
1
2
3
function rnd()
	return math.random(0, 255)
end
if you really don't give a shit about what it returns. Which you should.

- You're using way too much CPU and stuff with this ("0"):rep(3-#rndX). There's no use; you can use a for-loop, or even a function. Like I showed.

- To make this all shorter, you could use a way more compact script;

1
2
3
4
5
6
7
8
9
10
11
12
13
function lengthenString(s, length)
     while string.len(s) < length do
          s = '0'..s
     end
     return s;
end

function generateRandRGB()
     function rgb()
          return lengthenString(tostring(math.random(0, 255)))
     end
     return '©'..rgb()..rgb()..rgb()
end

and whenever you want a randomly generated color, you just use;

1
2
3
-- code
	color = generateRandRGB()
-- code

and if you cared enough, you could save it. For whatever reason.

You make admin tables inconvenient for the scripter

- Why the heck are there 3 different rank tables (admin, mod, vip), if they can be all put into one big one.

1
2
ranks = {
}

And; why use a loop, if you only wanna find out if a player is an admin or not?

1
2
3
4
5
6
7
8
9
10
11
12
13
ranks = {
	[usgn] = 1, --vip
	[usgn] = 2, --mod
	[usgn] = 3  --adm
}

And even worse, if the only difference between the three ranks is that they have a different tag, why not put those tags in a table?

[code]rankNames = {
	'(VIP)', -- 1
	'(Moderator)', -- 2
	'(Admin)' -- 3
}

This could shorten that huge text wall to this;

1
2
3
4
5
6
7
8
9
addhook('say', 'sayHook')

function sayHook(id, txt)
	local rank = ranks[player(id, 'usgn')]
	if rank then
		msg('©255255255'..rankNames[rank]..player(id, 'name')..' : '..txt)
		return 1;
	end
end

old Re: Upvalue of "color" (nil error)

Flacko
User Off Offline

Quote
'color' is a local variable (upvalue) which wasn't assigned to anything (a nil value) before it was used (attempt to concatenate). That's how you read this error msg.
I'd suggest to add a call to changecolor() right after you defined it.

old Re: Upvalue of "color" (nil error)

Rainoth
Moderator Off Offline

Quote
Thank you, user Flacko:
This is exactly what I needed. Simple explanation of what I did wrong and how to solve it.

@user Alistaire: I'm sorry but I'm still not at the level to understand what YOU write. I'm currently learning stuff with strings. I'll try to learn more about making good tables later.

Thanks for the script though, I'll bookmark it for the future
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview