Extremely weird ModuleScript behaviour

So, I am doing a commission for a person, and I am making a quest system. I am basically creating a simple “text reader” which just shows text on a GUI. But, for some reason when I grab data from the module script, the dictionary is not sorted the same way it’s sorted in the module script.
Visual:


Module script:

local Quests = {
	[1] = {
		["Lines"] = {
			["line1"] = "Welcome to BloxRim!",
			["Line2"] = "Your first task it going to be to kill a dummy.",
			["Line3"] = "Good luck!"
		},
		["Tasks"] = {
			["kill"] = "Dummy",
			["amount"] = 1
		}
	}
}

return Quests

Local script:

local QuestList = require(game.ReplicatedStorage.QuestList)
local plr = game.Players.LocalPlayer

for i, val in pairs(QuestList[plr.QuestFolder.QuestNumber.Value]["Lines"]) do
	
	for i = 0, #val, 1 do
		script.Parent.QuestText.Text = string.sub(val, 0, i)
		wait(0.05)
	end
	
	wait(2)
end

Any help would be appreciated

I’m not sure if this is true, but generic for loops seem to be rather random on their choices.

Try looping through it with a numerical for loop. Like so:

local QuestList = require(game.ReplicatedStorage.QuestList)
local plr = game.Players.LocalPlayer
local Lines = QuestList[plr.QuestFolder.QuestNumber.Value]["Lines"]

for LineIndex = 1, #Lines do
	local Line = Lines[LineIndex]

	for LetterIndex = 0, Line:len(), 1 do
		script.Parent.QuestText.Text = Line:sub(0, LetterIndex)
		wait(0.05)
	end
	
	wait(2)
end

Note: I won’t be available after the time of making this edit. I’ve got to sleep. Someone else will have to help you instead.

1 Like

Okay, I’ll try. But still, why is the core array in my computer’s RAM ordered differently that in my module script

Why make Lines a dictionary? Just convert it into an array* and use ipairs to iterate over it in the correct order (pairs iterates out of order).

*like so:

Lines = {
	"Welcome to BloxRim!",
	"Your first task it going to be to kill a dummy.",
	"Good luck!"
}
1 Like

Dictionaries doesn’t have a sorted index like array do. So the order most certainly will be mixed. And as other’s have said, you don’t need a dictionary for this use case, an array will be more than enough.

Tables with non-numerical indexes (i.e., no arrays) do not keep any order, so when you iterate through them the process seems random. Instead you can do something like this:

local QuestList = require(game.ReplicatedStorage.QuestList)
local plr = game.Players.LocalPlayer

local lines = QuestList[plr.QuestFolder.QuestNumber.Value]["Lines"]

for i = 1, 3 do
    local line = lines["Line" .. tostring(i)]

    for i = 0, #line, 1 do
		script.Parent.QuestText.Text = string.sub(line, 0, i)
		wait(0.05)
    end

    wait(2)
end
1 Like

I use dictionaries because the owner wants me to name each line.

FINAL ANSWER:

local QuestList = require(game.ReplicatedStorage.QuestList)
local plr = game.Players.LocalPlayer

function table_length(t)--getting the length of the dictionary
	local z = 0
	for i,v in pairs(t) do z = z + 1 end
	return z
end

for i = 1, table_length(QuestList[plr.QuestFolder.QuestNumber.Value]["Lines"]), 1 do
	
	local line = QuestList[plr.QuestFolder.QuestNumber.Value]["Lines"]["Line"..tostring(i)]--the current line
	
	for i = 0, #line, 1 do--looping through the line
		script.Parent.QuestText.Text = string.sub(line, 0, i)
		wait(0.05)
	end
	
	wait(2)
end