I would like to make a table of functions. Functions have their indexes represented as a level id (e.g. 100, 101, 200, 201, 202...) to change some level properties (lighting, gravity ect) So here is an example:
local functionsTable = {}
functionsTable[100] = function()
print("This is a function number 100")
end
functionsTable[101] = function()
print("This is a function number 101")
end
functionsTable[201] = function()
print("This is a function number 201")
end
You might ask, what's the problem? And a problem comes whenever a player connects to a server, and to apply all property changes in the past, my client script loops trough all of the functions in a table until it reaches current player's level. For example:
local functionsTable = {}
local plrLevelId = 200
functionsTable[100] = function()
print("This is a function number 100")
end
functionsTable[101] = function()
print("This is a function number 101")
end
functionsTable[201] = function()
print("This is a function number 201")
end
for i, v in pairs(functionsTable) do
print("Index " .. tostring(i))
if i <= plrLevelId then
functionsTable[i]() -- calling a function of previous levels to apply changes
else
print("Skipped " .. tostring(i) .. " - done")
break -- all functions of the previous and current level have been applied
end
end
--[[
Output:
Index 101
This is a function number 101
Index 201
Skipped 201 - done
Expected output:
Index 100
This is a function number 100
Index 101
This is a function number 101
Index 201
Skipped 201 - done
]]
Not only it skips a function that supposed to called in the order, but it called a function number 101, while function number 100 should be called first. What magic is this? How to go through all functions in a correct order that I assigned by index? Thank you in advance

1 comment
Very interesting solution, thank you