Hey! I was recently looking up what next() does because it’s a very confusing function, and I came across this post: Post and it explained it pretty well and when I tested out the example it worked perfectly, but when I tried naming the things in the dictionary differently they were printed out in weird orders. Any reason on why next() does this? Thanks!
You could sort it like this but I don’t think it’s practical unless absolutely necessary. I think this is the thing I confused myself with on table.sort
local myArray = {"apple", 2, true}
print(myArray[1]) --Prints 'apple'
local myDictionary = {
["Apple"] = "apple"
["2"] = 2
["true"] = true
}
print(myDictionary[1]) -- Prints nil
print(myDictionary["Apple"]) -- Prints 'apple' (same as myDictionary.apple)`
Something that helped me learn this was how Roblox’s objects are structured. They’re basically all arrays like ths:
local part = { --> locked with something like table.freeze internally (except for non-readonly properties)
Name = "Object",
ClassName = "Part"
...
}
The reason I don’t want to index the dictionary is because I have a quest system in my game and to make it easy to make new quests I need to use dictionaries but I don’t want to have a million if statements checking for what quest they’re(the player) on.
Can you go in detail for what you want to achieve? I’d like to help my best, but I need more information. We might need to move this to PMs if it becomes spammy back and forth replies, but I’m not sure about the rules on the forum regarding this.
You can simulate a dictionary order if you make a table with all indexes like this:
local myDict = {
['new'] = {};
['quick'] = {};
['cya'] = {};
}
----- example 1 -----
local indexes = {}
for i,v in pairs(myDict) do
indexes[#indexes + 1] = i
end
table.sort(indexes) -- should put it in an order similar to alphabetical (can't remember the name), iirc it has a bit of a difference but it's negligible when working with alphanumeric characters
-- now, if we use next on indexes, we can do something like this:
print(next(indexes)) --> cya
local _, value = next(indexes)
local newValue = dict[value] -- should return the entry whose key is "cya"
----- end of example 1 -----
-- if you want, you can also manually define them:
local indexes = {'new', 'quick', 'cya'} -- new should be the first entry, or index 1
local _, value = next(indexes) --> new
print(value) --> new again
local new = myDict[value] -- should return a value whose index is "new"
And so far I can figure out which quest they’re on but do to it being a dictionary I can’t just go to the next index afterwards. Currently I have a function to get the next quest and then change the player’s current quest to the next one:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local questEvents = events:WaitForChild("QuestEvents")
questEvents.GetQuest.OnServerEvent:Connect(function(player)
local questInfoModule = require(ServerStorage.Modules:WaitForChild("QuestInfo"))
local questOn = player.QuestInfo.QuestName.Value
local quest = questInfoModule.CataclysmicDev[questOn]
local nextQuest = ""
local num = 1
for i, v in pairs(questInfoModule.CataclysmicDev) do
if v == quest then
if num >= #questInfoModule.CataclysmicDev then
nextQuest = next(questInfoModule.CataclysmicDev)
else
nextQuest = next(questInfoModule.CataclysmicDev, i)
end
break
end
num += 1
end
print(nextQuest)
end)
You could order them like this? The only informality (imo) being that you need to index twice.
local quests = {
{{ -- surrounded in a table so it can be indexed with a key
quest = "hi"
}},
{{
quest = "hi2"
}}
}
print(quests[1]) --> {[1] = {quest = "hi"}}
print(quests[1][1]) --> {quest = "hi"}
Another solution is to store the quest “data” inside each position(for example name, value):
local quests = {
[1] = {"new", 2},
[2] = {"quick", "test"}
}
local quest = quests[math.random(1, #quests)]
local name = quest[1]
local value = quest[2]
print(name, value)
--this also can scale if you add more parameters for example quest[3] = the reward
I never thought about doing it like that! This should be easy enough to change. I’ll try it right now and hopefully this won’t be too hard to get working(currently I’m figuring out what quest to find in the dictionary with a StringValue inside of a folder in the player so I’ll just have to figure out how to find it without the StringValue which should be easy enough with a for loop). Thanks
Edit: IT WORKS!!!