How to make a function that finds which textlabel was last visible in a Frame?

I want to make a dna game sort of thing and everything is going good until I got to the part where the code needs to know what Textlabel was last visible in a certain Frame.
And I wanna know if there is a function like “last visible in certain place and choose between 2 text labels” Does anyone know anything? Thanks

1 Like

I’m unaware if there is a “lastVisible” function. Usually when I have to do something like this, I create a variable and then I set the variable to the item. That way you’d be able to refer to it later by simply getting the value of the variable.

1 Like

Here is a little script that I made (I wrote comments to explain what the code does):

local frameToCheck = script.Parent -- Reference to the frame
local timeTable = {} -- Table to store the time when the frame was visible
local changedConns = {} -- Table to store changed connections

function GetLastVisible()
	local lastVisible -- Set a variable to store the last visible text label
	
	for _, v in pairs(frameToCheck:GetChildren()) do -- Loop through the frame's children
		if v:IsA("TextLabel") and v.Visible then -- Check if the instance is a text label and make sure its visible
			if not lastVisible then lastVisible = v end -- Make the current frame if the "lastVisible" variable is nil
			
			local currentLastVisibleTime = timeTable[v.Name] -- Get the last visible time of the current textlabel
			local lowestLastVisibleTime = timeTable[lastVisible.Name] -- Get the last visible time of the textlabel stored in the "lasVisible" variable
			
			if currentLastVisibleTime > lowestLastVisibleTime then -- Check if the visible time of the current textlabel is greater than the one stored in the varible
				lastVisible = v -- if it is, set the lastVisible variable to the current text label.
			end
		end
	end
	
	return lastVisible -- return the lastVisible textlabel
end

for _, v in pairs(frameToCheck:GetChildren()) do -- Loop through all instances in the frame
	if v:IsA("TextLabel") then -- Check if the instance is a textlabel
		changedConns[v.Name] = v:GetPropertyChangedSignal("Visible"):Connect(function() -- check when the visibility property changes
			if v.Visible then -- check if the text label visibility was set to true
				timeTable[v.Name] = os.time() -- Update the time that the text label was set to visible.
			end
		end)
	end
end

changedConns[v.Name] = v:GetPropertyChangedSignal(“Visible”):Connect(function()
Edit: Make sure to disconnect this event after use.

1 Like

It worked! Thank you so much for the help!

1 Like