Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to convert table to string?

Hello, I have a little code which is for my game. Take a look at the script:

players = game.Players:GetPlayers()

if table.getn(players) >= 4 then
	crim = players[math.random(1, #game.Players:GetPlayers())]
	table.remove(players, players[crim.Name])
	plrs = tostring(players)
	print(plrs)
	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

1 comment

0

Don't forget to accept my answer if it works.

1 answer

✓ Accepted answer

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.

players = game.Players:GetPlayers()

if #players >= 4 then
    local num = math.random(1, #players)
    local crim = players[num]
    table.remove(players, num) -- # remove takes the index to remove not value
    print(unpack(players)) -- # print all the elements
    print(players)
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.

local arr = {"one", "two", "three"}
print(table.concat(arr, ", ")) --> one, two, three

However this is out of the scope of this answer so I will not explain any further.

5 comments

0

Thank you so much! I am new here, so please, can you tell me how to accept someone's answer?

0

I'll do it.

1

Thanks

1

Congrats on 2k

0

thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.