How to put text on parts without having duplicates

  1. What do you want to achieve? Keep it simple and clear!
    I want to have no duplicate text on these parts. What’s the best way to do this. I tried to do this in the loop below. I think i need to loop until i have a random answer thats not the actual answer, if i should do this how should i do it.

  2. What is the issue? Include screenshots / videos if possible!

is came up 2wice.


The first 2 lines of the script puts “banana” which is the answer on a random part. QuestionTable.answer is the actual answer. In the loop i tried to change the text of all parts except the actual answer to something random.

local randompart = parts[math.random(1, #parts)] --Random Part Chosen
randompart.SurfaceGui.TextLabel.Text = QuestionTable.answer --Changing the SurfaceGui.TextLabel.Text to QuestionTable.answer
	
for i, v in pairs(parts) do  --Looping through all the parts

		if v.SurfaceGui.TextLabel.Text ~= QuestionTable.answer then --if the SurfaceGui.TextLabel.Text isnt equal to the actual answer.
			local otheranswers = randomAnswerTable[math.random(1, #randomAnswerTable)] --get random answer from a table full of answers 
			
              if otheranswers ~= QuestionTable.answer then --if random answer is not actual answer.				
				v.SurfaceGui.TextLabel.Text = otheranswers --Change text to random answer.

Make an empty table, add texts in there which you have used, and when generating a new answer create a random text which isn’t in the previous generated things.
You can do that by using repeat untill:

While #usedtexttable ~= <amount of answers you need> do
Repeat
Local text = randomanswertable[what you have there]
Until not table.find(usedtexttable, text)
Table.insert(usedtexttable, text)
Handle setting your surfaceguis
End

Please note that this is pseudocode and needs to be adapted for you to work

1 Like

you should probably add a value in the table for the other strings saying false or true

local randomAnswerTable = {
    [1] = {
        "Word",
        false,
    },
}

and check if it is false or true. You would only need to add a line or two of code for this.

if otheranswers[1] ~= QuestionTable.answer and not otheranswers[2] then 				
v.SurfaceGui.TextLabel.Text = otheranswers[1]
otheranswers[2] = true
2 Likes

I may be a banana but it didnt work. Gona quickly add somthing.

local randompart = parts[math.random(1, #parts)] 
randompart.SurfaceGui.TextLabel.Text = QuestionTable.answer -- Sets actual answer on to random part first.

	for i, v in pairs(parts) do  
		if v.SurfaceGui.TextLabel.Text ~= QuestionTable.answer then 
			
           local otheranswers = randomAnswerTable[math.random(1, #randomAnswerTable)] --get random answer
			table.insert(anemptyTable, otheranswers) --Insert random answer to a empty table.
			for i, v in pairs(anemptyTable) do --Loop through table 
				if otheranswers == v then
					repeat
						wait(0.1)
						local otheranswers = randomAnswerTable[math.random(1, #randomAnswerTable)] -- Keep repeating till its not in table.
					until not not table.find(anemptyTable, otheranswers)

				end
			end
			
				
				v.SurfaceGui.TextLabel.Text = otheranswers --Set random answer which shouldnt be duplicate.
1 Like

You have until not not, that wont work because it will never be in the table before you added it.
Next to that you didn’t follow the order that I said. Iterating over an empty table wont work, and you never created the empty table.

1 Like

The empty table is above my script but its messy.


messy
Removed not not

im a banana…