Is there a more efficient way other than using for loops for my dictionary?

Is there a more efficient way other than using for loops for my dictionary

What’s goin on:

So I have this module script that basically plays a little GUI animation. The style of the animation is kinda like an old console on a computer. The dictionary shows every line that will be displayed on the screen. This is my first actual Roblox game and I want to make all the scripts and everything very efficient and not a victim of lag or bugs. So at the bottom of my module script function, I have two for loops, the second one embedded into the first one. The first one is the one I’m worried about.

What I need:

I don’t really think the second for loop is needed since there is only one key and one value inside of each dictionary nested into the first one. But I don’t exactly know if there is a better way to do this. Using a for loop takes a bit more space and I don’t want that. Is there a more optimized way for this?

Script:

local rp = game:GetService("ReplicatedStorage")
local sounds = rp:WaitForChild("Sounds")

local LoadingModule = {}

-- You can do a max of 18 messages; Pixel limit
local msgs = { -- 1 weight = 0.2 seconds
	[1] = {["Loading"] = 12},
	[2] = {["Made by MonsterTrident"] = 5},
	[3] = {["GUIS Loaded"] = 0},
	[4] = {["Scripts Loaded"] = 0},
	[5] = {["Player Loaded"] = 1},
	[6] = {["Building Assets Loaded"] = 0},
	[7] = {["All Instances Loaded"] = 1},
	[8] = {["Drinking Coffee.."] = 4},
	[9] = {["Getting Datastore Data"] = 1},
	[10] = {["Building World"] = 0},
	[11] = {["Advertising the Game"] = 0},
	[12] = {["Grabbing Sound Effects"] = 0},
	[13] = {["Game Phase: [PRE-ALPHA]"] = 4}, 
	[14] = {["Have Fun :)"] = 0}
}

function LoadingModule.LoadScreen(plr: Player, beforeTime: number)
	local plrGUI = plr:WaitForChild("PlayerGui")
	local loadingScreen = plrGUI:WaitForChild("LoadingScreen")
	local loadingScreenFrame = loadingScreen:WaitForChild("Frame")
	loadingScreen.Enabled = true
	local ConsoleText = loadingScreenFrame:WaitForChild("ConsoleText")
	local glitchFrame = loadingScreenFrame:WaitForChild("GlitchFrame")
	local glitchFrameHeavy = loadingScreenFrame:WaitForChild("GlitchFrameHeavy")
	task.wait(beforeTime) -- START OF THE ANIMATION
	for _, messageData in ipairs(msgs) do
		for message, weight in pairs(messageData) do
			print(message) -- for testing purposes
		end
	end
end

return LoadingModule

Here is the line I’m talking about just incase I wasn’t clear (part of the second for loop):

for message, weight in pairs(messageData) do

Ask me any questions!

1 Like

ipairs, pairs is all obsolete.

for _, messageData in msgs do

will perform just as good as ipairs etc.

Also you could do

for _,messageData in msgs do
message, weight = next(messageData)
1 Like

Why not lay it out such that the first index is the string and the second index is the weight?

local msgs = { -- 1 weight = 0.2 seconds
	[1] = {"Loading", 12},
	[2] = {"Made by MonsterTrident", 5},
	[3] = {"GUIS Loaded", 0},
    -- etc

then message would be messageData[1] and weight would be messageData[2], no need for a second loop that way

2 Likes

Appreciate the help! I’m still new to some stuff in Roblox LUA but yeah it’s fun to learn new stuff. I asked the Roblox assistance bot for help and I wasn’t satisfied that it was suggesting to use the same exact thing I had in my code before. Anyways I have a follow up question but since I’m busy at the moment it will have to wait. Hoping you or someone else could answer it later.

Hi,

I believe that you can just use a nested table for storing your messages:

local msgs = { -- 1 weight = 0.2 seconds
	{"Loading", 12},
    {"Made by MonsterTrident", 5},
	{"GUIS Loaded", 0},
-- ...

this way you’ll only need one for loop to loop through the main table:

for i=1, #msgs do
    local text = msgs[i][1]
    local displayTime = msgs[i][2]
end
1 Like

All of these solutions are super helpful and really show that there isn’t just one good answer. I assigned the solution tag to the one that made the most sense, thanks again guys!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.