Hello, I have a little code which is for my game. Take a look at the script:
1 | players = game.Players:GetPlayers() |
2 |
3 | if table.getn(players) > = 4 then |
4 | crim = players [ math.random( 1 , #game.Players:GetPlayers()) ] |
5 | table.remove(players, players [ crim.Name ] ) |
6 | plrs = tostring (players) |
7 | print (plrs) |
8 | print (players) |
...
This is in a while true do loop. So I am selecting one player as a criminal, and then removing it from the 'players' table, to make everyone else a police. But when I try to convert the table to a string (to test if it worked), it gives me 'table:' and random characters, such as 13E2F21, both times: print(plrs) and print(players).
My question is, how to convert a table to a string? Sorry for my bad English EDIT: I have tried to comment table.remove(......) with --, but it still not converted the table to string
It's not random; it's the memory address of the table! If you want to print all the elements of the array, use unpack()
since it returns all the elements of a array.
Additionally, you should use the length operator #
to get the table length.
1 | players = game.Players:GetPlayers() |
2 |
3 | if #players > = 4 then |
4 | local num = math.random( 1 , #players) |
5 | local crim = players [ num ] |
6 | table.remove(players, num) -- # remove takes the index to remove not value |
7 | print ( unpack (players)) -- # print all the elements |
8 | print (players) |
9 | end |
It would also be prudent to mention table.concat
but this only works for string and number elements, and instances are not strings or numbers, so this will fail.
1 | local arr = { "one" , "two" , "three" } |
2 | print (table.concat(arr, ", " )) --> one, two, three |
However this is out of the scope of this answer so I will not explain any further.