Passing table to function and getting value from given table returns nil

My brain is melting. I think there’s something wrong with me or the Roblox engine.

So, here we have a UI, I want to tween every red frame of the UI: up and down. To move them in those directions, I of course made main frames move (they are not transparent) and visualized frames (they are transparent), so I could get custom positions without changing any values in the code.

The frames (from left to right) that are red and not transparent are “First”, “Middle” and “Second”.

And after the UI marking, the fun begins in the coding process (code parts that need my and your investigation are marked with comments):

repeat
	wait()
until game:IsLoaded()

local TS = game.TweenService

local Player = game.Players.LocalPlayer
local PlayerGUI = Player.PlayerGui

local UI = PlayerGUI:WaitForChild("Loading")
local Back = UI.Back -- The frame which is on the background, of course.
local Middle, First, Second = Back.Middle, Back.First, Back.Second -- Non-transparent frames.

local MiddlesFolder, FirstFolder, SecondFolder = Middle.ExpectionPositions, First.ExpectionPositions, Second.ExpectionPositions -- Folders inside each frame, folder have transparent frames: "Up" and "Down"

-- Tables for having visualized positioning for frames
local Positions = {
	MiddlePhases = {
		UP = MiddlesFolder.Up,
		DOWN = MiddlesFolder.Down
	};

	FirstPhases = {
		UP = FirstFolder.Up,
		DOWN = FirstFolder.Down
	};

	SecondPhases = {
		UP = SecondFolder.Up,
		DOWN = SecondFolder.Down
	};
}

-- Check if tables does exist after creating
for _, tab in pairs(Positions) do
	if tab then
		print("Gotten "..tostring(tab))
	elseif tab == nil then
		error("Table was not created, maybe.")
	end
end

-- Function to loop the tweening
function moveFrame(frame, phases)
	if phases == nil then
		print("Could not get the table :(")
		return
	end

	local currentPositionIndex = 1
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)

	while true do

		local currentPhase = phases[currentPositionIndex]
		print("Inspecting "..tostring(phases)) -- Table itself shows the special id,
		print("Checking phase: "..tostring(currentPhase)) -- value from table gives nil.
		local goal = {
			Position = UDim2.new(currentPhase.X.Scale, currentPhase.X.Offset, currentPhase.Y.Scale, currentPhase.Y.Offset) -- Here, when it tries to get first value from table - it says that the returned value was nil.
		}
		print("Setting goal to: "..tostring(frame))
		local tween = TS:Create(frame, tweenInfo, goal)
		tween:Play()
		print("Playing Tween of frame: "..tostring(frame))
		tween.Completed:Wait()
		print("Finished Tween of frame: "..tostring(frame))
		currentPositionIndex = currentPositionIndex % #phases + 1
	end
end

local Coroutine1, Coroutine2, Coroutine3 = coroutine.create(moveFrame(Middle, Positions.MiddlePhases)), coroutine.create(moveFrame(First, Positions.FirstPhases)), coroutine.create(moveFrame(Second, Positions.SecondPhases))

coroutine.resume(Coroutine1)
coroutine.resume(Coroutine2)
coroutine.resume(Coroutine3)

The result of running this code leads me to such output:

I honestly don’t know why table gives the id when print, but after another check if table has value from an index. I need a little bit of assistance.

1 Like

Dictionaries do not have number indexes. You would have to index the dictionary using the key:

phases[1] -- Would return nil
phases["UP"] -- Would return MiddlesFolder.Up
2 Likes

Alright, thanks, I’ll refresh my mind really quickly and let you know if such method worked.

1 Like

For some strange reasons, my Positions table could not get any value by using index.

I implemented your method in another way (really dumb) to pick up a value by using another table with key names: “UP” and “Down” and increasing it’s index. I decided to update the move frame function.

function moveFrame(frame, phases)
	if phases == nil then
		print("Could not get the table :(")
		return
	end

	local PositionIndexes = {"UP", "DOWN"}
	local currentPositionIndex = 1
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)

	while true do

		local currentPhase = phases[PositionIndexes[currentPositionIndex]]
		print("Inspecting "..tostring(phases))
		print("Checking phase: "..tostring(currentPhase))
		local goal = {
			Position = UDim2.new(currentPhase.Position.X.Scale, currentPhase.Position.X.Offset, currentPhase.Position.Y.Scale, currentPhase.Position.Y.Offset)
		}
		print("Setting goal to: "..tostring(frame))
		local tween = TS:Create(frame, tweenInfo, goal)
		tween:Play()
		print("Playing Tween of frame: "..tostring(frame))
		tween.Completed:Wait()
		print("Finished Tween of frame: "..tostring(frame))
		currentPositionIndex = currentPositionIndex % #phases + 1
	end
end


AND IT’S WORKING. Not really as I expected to red frames move up and down but still - IT WORKED.

1 Like

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