Need to randomize without duplicates

  1. What do you want to achieve?

I want to place answers on these parts without any duplicates.
image

  1. What is the issue?
    I’m a banana who is for some reason unable to place answers on a wall without any duplicates.

  2. What solutions have you tried so far?

Heres my last try

local sss = game:GetService("ReplicatedStorage")
local quest = require(sss.question) 

local QuestionText = script.Parent
local text

local parts = game.Workspace:WaitForChild("Answers"):GetChildren()


local randomAnswerTable = {}





for i = 1, 7 do
	
	for i, v in pairs(quest) do
		table.insert(randomAnswerTable, v.answer) --insert all answers into table
	end
	
	local QuestionTable = quest[i]
	text = QuestionTable.question
	local length = string.len(text) --Question comes up on gui
	
	script.Sound:Play()
	
	for i = 1, length do
		QuestionText.Text = text:sub(1, i)
		wait(0.04)
		if i > 35 then
			script.Sound:Stop() --for stuff to com on gui
		end
	end
	
	print(QuestionTable.answer)
	local randompart = parts[math.random(1, #parts)] --random part
	randompart.SurfaceGui.TextLabel.Text = QuestionTable.answer --change that parts text to right answer.	
	
	--Am i doing somethin obviously wrong in the loop below. 

	for i, v in pairs(parts) do  --all parts
		if v ~= randompart then -- if the part isnt the random part chosen above
			
			table.remove(randomAnswerTable, #QuestionTable.answer) --remove actual answer from table. THIS IS WRONG, WAT IS RIGHT
			local otheranswers = randomAnswerTable[math.random(1, #randomAnswerTable)] --random answer from table
			table.remove(randomAnswerTable, randomAnswerTable[otheranswers]) --remove random answer from table
			
				
				v.SurfaceGui.TextLabel.Text = otheranswers --the parts text becomes other answer
				
			end
		end
	
	for i, v in pairs(randomAnswerTable) do -- empty the table
		
		print(v)
		v = nil
		
	end
	


1 Like

If it was me I would create a separate table and add all the randomized values into there, then check for any duplicated. If there are some, delete them and put the remaining words into the randomAnswersTable.

For example:

local answerTable = {}
local checkingTable = {}

for i = 1, 7 do
    for _, word in pairs(module thing) do
        table.insert(checkingTable, word.answer)

        if table.find(checkingTable, word.answer) > 1 then
            table.remove(word.answer)
        end

        table.insert(answerTable, checkingTable)
        -- do stuff
    end
end

I have never tried this before so this could be wrong.

3 Likes

Let’s say you have the original table consisting of multiple answers. The code you want to create is to populate a duplicate array of the answers. This array will be selecting the answers randomly and subsequently removing one from the array. This will avoid duplicates. The same code will be used for every new “question”, assuming that’s how the system should perform.

3 Likes

Tried and couldnt do it :slightly_frowning_face:

and heres my entire loop

local sss = game:GetService("ReplicatedStorage")
local quest = require(sss.question) 

local QuestionText = script.Parent
local text

local parts = game.Workspace:WaitForChild("Answers"):GetChildren()


local randomAnswerTable = {}






for i = 1, 7 do -- for questions 1-7
	
	for _, v in pairs(quest) do
		table.insert(randomAnswerTable, v.answer) --insertin all answers
		print("THIS IS INSERTED"..v.answer)
	end
	
	
	local QuestionTable = quest[i]
	
	
	text = QuestionTable.question
	local length = string.len(text) --Question comes up
	
	script.Sound:Play()
	
	for i = 1, length do
		QuestionText.Text = text:sub(1, i)
		wait(0.04)
		if i > 35 then
			script.Sound:Stop()
		end
	end
	
	
	print(table.remove(randomAnswerTable, table.find(randomAnswerTable, QuestionTable.answer))) -- remove the answer the loop is on from table
	
	
	for _, v in pairs(parts) do  --for all parts

			print(QuestionTable.answer)
			
			local otheranswers = randomAnswerTable[math.random(1,#randomAnswerTable)] --random answer
			
			table.remove(randomAnswerTable, table.find(randomAnswerTable, otheranswers)) -- remove random from the table

			v.SurfaceGui.TextLabel.Text = otheranswers --text = random
				
			
	end
	
	print(QuestionTable.answer)
	local randompart = parts[math.random(1, #parts)] --Random part
	randompart.SurfaceGui.TextLabel.Text = QuestionTable.answer --the right answer on a random part
	print(QuestionTable.answer)
	
	for _, v in pairs(randomAnswerTable) do
		
		print("THIS IS DELETED"..v)
		v = nil
		
	end
	
	local therightanswer = false
	
	for _, v in pairs(parts) do
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and therightanswer == false then
				if v.SurfaceGui.TextLabel.Text == QuestionTable.answer then
					
					therightanswer = true
					v.CanCollide = false
					v.Transparency = 1
					wait(2)
					v.CanCollide = true
					v.Transparency = 0.8
					
				end
			end
			
			
		end)	
	end
	
	 
	
	repeat wait() until therightanswer == true -- waiting till answer has been chosen, then next question
	
	print("YESYESYES")
	therightanswer = false
	
	
	print(QuestionTable.question .. "\n" .. QuestionTable.answer)

end


With the code above
6 duplicated in dis vid and i dont no how
robloxapp-20210708-1822522.wmv (2.9 MB)

1 Like

this works, thanks.

Heres the test table in the module i used

local testTable = 
{
		[1] = {question = "q1", answer = "a1"},
		[2] = {question = "q2", answer = "a2"},
		[3] = {question = "q3", answer = "a3"},
		[4] = {question = "q4", answer = "a4"},
		[5] = {question = "q5", answer = "a5"},
		[6] = {question = "q6", answer = "a6"},
		[7] = {question = "q7", answer = "a7"},
		[8] = {question = "q8", answer = "a8"}
	
	
	
}

return testTable

and here the script that didnt remove the answer to the 2nd question for som reason

local req = game:GetService("ReplicatedFirst")

local getTABLEH = require(req.testinModule)

local txtgui = script.Parent

local twaTable = {}


local randomans

for i = 1, 2 do
	local QuestionTable = getTABLEH[i] --getin tables keys
	print(QuestionTable.question .. "\n" .. QuestionTable.answer)
	
	for _, v in pairs(getTABLEH) do
		table.insert(twaTable, v.answer) --insertin all answers
		print("THIS IS INSERTED"..v.answer)
	end
	
	randomans = twaTable[math.random(1,#twaTable)]
	print("thiswas printed"..randomans)
	print(table.remove(twaTable, table.find(twaTable, randomans)))
	
	for _, v in pairs(twaTable) do
		print("THIS IS DELETED"..v)
		v = nil
	end
	
end

and heres wat worked

local req = game:GetService("ReplicatedFirst")

local getTABLEH = require(req.testinModule)

local txtgui = script.Parent

local twaTable = {}

local sndTable = {}

for _, v in pairs(getTABLEH) do
	table.insert(twaTable, v.answer) --insertin all answers
	print("THIS IS INSERTED"..v.answer)
end

local randomans

for i = 1, 4 do
	local QuestionTable = getTABLEH[i] --getin tables keys
	print(QuestionTable.question .. "\n" .. QuestionTable.answer)
	
	randomans = twaTable[math.random(1,#twaTable)]
	print(randomans)
	table.insert(sndTable, randomans)
	table.remove(twaTable, table.find(twaTable, randomans))
	
	
	
end


for i, v in pairs(sndTable) do --randomans should be in here
	print(v)
end

for i, v in pairs(twaTable) do --randomans should not be here
	print(v)
end
2 Likes