Forum

> > CS2D > Scripts > random name from table...
Forums overviewCS2D overview Scripts overviewLog in to reply

English random name from table...

14 replies
To the start Previous 1 Next To the start

old random name from table...

loldlold123
User Off Offline

Quote
İ need table,

STEP1:
When your playerjob[id]=1
add player's name to table
STEP2:
math.random table values
parse("hudtxt2 "..id.." "..random 1 name from table.."")
STEP3:
when player leave from game,remove his name from table if his name there in table.


i need this system

old Re: random name from table...

TimeQuesT
User Off Offline

Quote
Why you don't script it by yourself!?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
table = {};

function vAddPlayer()
for id=1,32 do
if (player(id,"exists")) then
table[id] = player(id,"name");
else
table[id] = nil;
end
end
end

function vSelectPlayer()
iRand = math.random(1,32);
if (table[iRand]!=nil) then
--code for hud
else
vSelectPlayer();  //be warned if there is no player on the server it can crash ;)
end
end

old Re: random name from table...

Flacko
User Off Offline

Quote
You could use player(0,'table') instead of guessing ids so you don't have to check all that stuff

And
Quote
parse("hudtxt2 "..id.." "..random 1 name from table.."")

wtf...

old Re: random name from table...

TimeQuesT
User Off Offline

Quote
Check the amount of players on the server before recalling the function

@Flacko -->
I just use a good followable way (if you don't care about the length of you script you can use my way, else Flackos ;))

old Re: random name from table...

Kel9290
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
awesometable={}
-- step 1
if playerjob[id]==1 then
	table.insert(awesometable,id)
	print(id.." inserted.")
end
--step 2
	yeah=#player(0,"table")
	dat = math.random(1,yeah)
	print("random - "..dat)
	parse("something "..player(dat,"name").." <---")
--step 3
addhook("leave","epicleave")
function epicleave(id)
	table.remove(awesometable,id)
	print(id.." removed.")
end

btw, tablestutorial.
Spoiler >

old Re: random name from table...

IWhoopedPythagoras
BANNED Off Offline

Quote
user Kel9290 has written
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
awesometable={}
-- step 1
if playerjob[id]==1 then
	table.insert(awesometable,id)
	print(id.." inserted.")
end
--step 2
	yeah=#player(0,"table")
	dat = math.random(1,yeah)
	print("random - "..dat)
	parse("something "..player(dat,"name").." <---")
--step 3
addhook("leave","epicleave")
function epicleave(id)
	table.remove(awesometable,id)
	print(id.." removed.")
end

btw, tablestutorial.
Spoiler >



This is so wrong.


1
2
3
4
5
--step2
yeah = player(0,"table")
dat = yeah[math.random(1,#yeah)]
print("random - "..dat)
parse("something "..player(dat,"name").." <---")

You should read the tables tutorial yourself.

old Re: random name from table...

Ultimate programmer
BANNED Off Offline

Quote
@TimeQuesT write scripts with Tabs!Scripts without tabs was made by loosers!Use special programs as Programmer's Notepad.

Random:
1
2
3
4
5
6
7
8
number=player(pl,"team")+pl*37...--add where all you want
randomcount=5 --example random:1,2,3,4,5
	while (number>randomcount) do
	number=number-randomcount
	end
	--it's mean number mod randomcount or
	--number%randomcount
answer=number

As function:
1
2
3
4
5
6
7
8
function random(number,randomcount)
	while (number>randomcount) do
	number=number-randomcount
	end
return number
end

some=random(player(pl,"team")+pl*37,5)

old Re: random name from table...

EngiN33R
Moderator Off Offline

Quote
user IWhoopedPythagoras has written
user Kel9290 has written
-snip-


This is so wrong.


1
2
3
4
5
--step2
yeah = player(0,"table")
dat = yeah[math.random(1,#yeah)]
print("random - "..dat)
parse("something "..player(dat,"name").." <---")

You should read the tables tutorial yourself.


Okay, how is your code more right than his? What's wrong with his step 2?

old Re: random name from table...

IWhoopedPythagoras
BANNED Off Offline

Quote
user EngiN33R has written
user IWhoopedPythagoras has written
user Kel9290 has written
-snip-


This is so wrong.


1
2
3
4
5
--step2
yeah = player(0,"table")
dat = yeah[math.random(1,#yeah)]
print("random - "..dat)
parse("something "..player(dat,"name").." <---")

You should read the tables tutorial yourself.


Okay, how is your code more right than his? What's wrong with his step 2?


Because of the following.

the player(0,"table") will return a table like this:


yeah = {1=1,15=15,31=31}

assuming the players with id 1,15 and 31 are alive.

however #yeah will return 3...
math.random(1,3) will return a id between 1 and 3. but id 2 and 3 is not alive.

old Re: random name from table...

EngiN33R
Moderator Off Offline

Quote
But player(0,"table") returns the following, if I'm not mistaken:

1
yeah = {[1]=1,[2]=15,[3]=31}

but not

1
yeah = {[1]=1,[15]=15,[31]=31}

No? If it does so indeed, then your code makes sense, but if it returns a table like you've written it (example #2 I provided) then your code won't work as well.

old Re: random name from table...

IWhoopedPythagoras
BANNED Off Offline

Quote
You are right. But my code holds.
I checked it and the table is automatically filled.


So it's like

1
yeah = {[1] = 1,[2]=5,[3]=6}
When player with id 5 leaves it will be:

1
yeah = {[1] = 1, [2] = 6}

when someone joins, he gets the lowest available id.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview