Pick random from dictionary with no repeats

I’m trying to make a daily quest system and I have all the quests in a dictionary modulescript. The idea is that there will be 6 quests that reset every 24 hours and I will have these quests be picked at random and go into a table with no repeats so the same quest isn’t there twice but don’t really know how to do the random quest picking part.

What I have so far:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local QuestsDaily = require(ReplicatedStorage.Databases.QuestsDaily)

local dailyQuestsTable = {}

--putting just the name in the table for now
for i = 1, 2, 1  do
	table.insert(dailyQuestsTable, QuestsDaily["Power Quests"][math.random(1, #QuestsDaily["Power Quests"])].Name)
	table.insert(dailyQuestsTable, QuestsDaily["Punching Quests"][math.random(1, #QuestsDaily["Punching Quests"])].Name)
	table.insert(dailyQuestsTable, QuestsDaily["Gathering Quests"][math.random(1, #QuestsDaily["Gathering Quests"])].Name)
end

print(dailyQuestsTable)

The output:

[1] = "Daily Training II",
[2] = "Daily Punching III",
[3] = "Daily Gathering II",
[4] = "Daily Training II", --repeat of 1
[5] = "Daily Punching IIII",
[6] = "Daily Gathering II" --repeat of 3
1 Like

I have edited ur script, so the loop continues until dailyQuestsTable has 6 quests, before adding a quest to dailyQuestsTable, it checks if the quest is already in the table using table.find, if its not then add it.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local QuestsDaily = require(ReplicatedStorage.Databases.QuestsDaily)

local dailyQuestsTable = {}
local categorys = {"Power Quests", "Punching Quests", "Gathering Quests"}

while #dailyQuestsTable < 6 do
	local category = categorys[math.random(1, #categorys)]
	local quest = QuestsDaily[category][math.random(1, #QuestsDaily[category])].Name

	if not table.find(dailyQuestsTable, quest) then
		table.insert(dailyQuestsTable, quest)
	end
end

print(dailyQuestsTable)
1 Like