Heya,
Currently making a jailbreak-like quest system, but this system is being annoying. Even ChatGPT can’t fix it, lol.
The problem is that as said, when duplicate quest rolls (which is fine), it gets assigned the same ID, no matter if I set a custom variable, or just use index of the for loop. ChatGPT and I both tried to make a table that stores numbers that have already been used so the game gets forced to skip that number and go to the next one (e.g. 2 is used, force to 3). Even that didn’t work. If anyone could help out that’d mean a lot to me.
Problem can be seen in screenshot.
In screenshot 1 we see that 1 quest has been rolled 3 times ranging at 4-6 so it got the ID 6 for 3 times. The other screenshots are self explanatory and show more information. On the same quest, the id is the same. I want to avoid this and get a simple 1 2 3 4 5 6 order, so if 2 and 5 are the same quest, they still independently get 2 and 5, and not 1, 3, 4, 5, 5, 6 (or 1, 5, 3, 4, 5, 6)
Here’s my code so far, I’ll briefly explain what all functions do.
Print: loops through t: table, and then print the id, name, desc and xpreward.
Sort: sorts the table from low to high (or A to Z), str: string can be set to anything, so if you want to sort on description, you just set str: string to “description”
Assign: this is one of hundred things i tried: loop through table to assign a custom ID to value (quest) and then str:string which is ID, so quest[“id”] == quest.id, we set it to the custom number. when we loop through table we add + 1 each time to ‘number’, but on the same quest it just skips, lol
Randomize: if statement is true we allow duplicate quests, i dont show the ‘false’ part, because that works perfectly fine and doesn’t need adjustments., we loop it for 6 times so we get 6 quests and we put them in a questIndices table so we can insert them into the openQuests table on the next loop, and then we have our 6 randomized (duplicate allowed) quests table. we try to assign id, but that’s the main problem, and print everything (id, name, desc, xpreward) and then return it so we can use it in another script.
function Quests:Print(t)
local prename = "[Season System]"
for i, quest in pairs(t) do
print(prename.." id = "..quest.id.." | name = "..quest.name.." | description = "..quest.description.." | xpreward = "..quest.xpreward)
end
end
function Quests:Sort(t, str)
table.sort(t, function(a, b)
return a[str] < b[str]
end)
return t
end
function Quests:Assign(t, str)
local number = 0
for i, value in pairs(t) do
number += 1
value[str] = number
end
return Quests:Sort(t, str)
end
function Quests:Randomize(Player: Player, Statement: boolean)
--set STATEMENT: boolean to TRUE if duplicate quests are allowed
--set STATEMENT: boolean to FALSE if duplicate quests are NOT allowed
if Statement == true then
local questIndices = {}
for i = 1, 6 do
local randomIndex = math.random(1, #PossibleQuests)
questIndices[i] = randomIndex
end
local openQuests = {}
for i, index in pairs(questIndices) do
local quest = PossibleQuests[index]
--quest.id = i [tried this previously, but that just gives the same id if its the same quest]
table.insert(openQuests, quest)
end
Quests:Assign(openQuests, "id")
Quests:Print(openQuests)
return openQuests
end
end
Help is welcome, thanks!