Hello!
Basically I made a few iterator functions- ripairs, apairs, and rapairs (reverse ipairs, alphabetic (actually lexical) pairs, reverse alphabetic pairs). Basically I’m looking for a seamless implementation which generally is going well except when I am calling a function from another function, the “global” doesn’t exist.
Here’s the module:
local module = {
ripairs = function(tbl: {any})
local index = #tbl
return function()
local initIndex = index
local initValue = tbl[index]
if initValue then
index -= 1
return initIndex, initValue
end
end
end;
apairs = function(dict: {[any]: any}, sortFunction: ((any, any) -> boolean)?)
local sortedIndexes = {}
for i,v in pairs(dict) do
table.insert(sortedIndexes, i)
end
table.sort(sortedIndexes, sortFunction)
local index = 1
return function()
local initIndex = sortedIndexes[index]
local initValue = dict[initIndex]
if initIndex then
index += 1
return initIndex, initValue
end
end
end;
rapairs = function(dict: {[any]: any}, sortFunction: ((any, any) -> boolean)?)
local sortedIndexes = {}
for i,v in pairs(dict) do
table.insert(sortedIndexes, i)
end
table.sort(sortedIndexes, sortFunction)
local index = #sortedIndexes
return function()
local initIndex = sortedIndexes[index]
local initValue = dict[initIndex]
if initIndex then
index -= 1
return initIndex, initValue
end
end
end;
pairs = pairs;
ipairs = ipairs;
}
return setmetatable(module, {__call = function() -- note the __call metamethod
local fenv = getfenv(2)
for i,v in pairs(module) do
fenv[i] = v
end
end})
Then to “deploy” it, require the module and call the table, invoking the __call metamethod:
require(modules:WaitForChild('Utility'))()
However, when I call a function within a function, for example here:
local function effect()
for i,v in rpairs(order) do -- attempt to call a nil value
-- not important
local container = script.Parent:FindFirstChild(v .. 'Container')
if container then
local mousePositionOnButton = Vector2.new(mouse.X - container.AbsolutePosition.X, mouse.Y - container.AbsolutePosition.Y)
local blackCircle = Instance.new('Frame')
local uiAspectRatioConstraint = Instance.new('UIAspectRatioConstraint')
uiAspectRatioConstraint.Parent = blackCircle
uiAspectRatioConstraint.AspectRatio = 1
blackCircle.BackgroundColor3 = Color3.new()
blackCircle.AnchorPoint = Vector2.new(0.5,0.5)
blackCircle.Size = UDim2.new()
blackCircle.Position = UDim2.fromOffset(mousePositionOnButton.X, mousePositionOnButton.Y)
blackCircle.Parent = container:FindFirstChild('InitEffect')
tweenService:Create(blackCircle, TweenInfo.new(1), {Size = UDim2.new(0,50,0,50)}):Play()
end
end
end
------------------------
local function out()
for i,v in ripairs(order) do --> ok
local container = script.Parent:FindFirstChild(v .. 'Container') :: Frame
if container then
coroutine.wrap(function()
-- not important
local initEffect = container:WaitForChild('InitEffect') :: Frame
local initAbsoluteSize = container.AbsoluteSize.X
initEffect.Position = UDim2.new(0,0,0,0)
initEffect.AnchorPoint = Vector2.new(0,0)
tweenService:Create(initEffect, tweenInfo, {Size = UDim2.new(1,0,1,0)}):Play()
---------------------------------
effect() -- note this is where I'm calling the second function
---------------------------------
-- also not important
task.wait(0.25)
for i,v in pairs(container:GetChildren()) do
if v:IsA('GuiObject') and v ~= initEffect then
v.Visible = false
end
end
initEffect.AnchorPoint = Vector2.new(1,0)
initEffect.Position = UDim2.new(1,0,0,0)
tweenService:Create(initEffect, tweenInfo, {Size = UDim2.new(0,0,1,0)}):Play()
end)()
task.wait(0.1)
end
end
end
Is there any way to get around the error without having to manually define the functions, and without having to index the functions through the module?