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

Why are these being used more than once?

I want each letter to only be used once when it prints on the last line, but it does some letters more than once, why?

	local Pool={"a","b","c"}
	Pool[1] = Pool[math.random(1,#Pool)]
	local OM1 = Pool[1]
	table.remove(Pool, 1)

	Pool[1] = Pool[math.random(1,#Pool)]	
	local OM2 = Pool[1]
	table.remove(Pool, 1)
	
	Pool[1] = Pool[math.random(1,#Pool)]	
	local OM3 = Pool[1]
	table.remove(Pool, 1)
	
	print(OM1,OM2,OM3)

1 answer

Each time, you're replacing the first element with a random one. This eliminates the first one at each step from being truly chosen later. You then eliminate that copy instead of the original.

That will look like this:

{a, b, c}
{c, b, c} -- Pool[1] = Pool[3]
{b, c} -- table.remove(Pool, 1)
{c, c} -- Pool[1] = Pool[2]
{c} -- table.remove(Pool, 1)

Here is how you remove a random thing from a list:

local O1 = table.remove(Pool, math.random(#Pool))

Note that the 1, is optional in math.random.

5 comments

0

I'm sorry, but where would I use this? I don't quite understand

0

Sorry, had a typo in the correct line. It's also a little clearer now.

0

I still don't understand... Do I replace my lines 4,8,12 with this?

0

I give a line explaining how to get and remove a random thing from a list. It defines O1. You can infer that would replace the stuff you use to modify Pool and get OM1. You can then infer that since 1 is not part of it, you can reuse this same line to make OM2 and OM3.

0

Oh okay. Thank you so much!

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