Get InputBegan to connect to each individual frame

I’m having problems checking when a player presses a key and it checking for a UI’s element.

local function createTile(tileDirection)
	local TilePosition = Bottom:FindFirstChild(tileDirection)
	if not TilePosition then return end
	
	local Tile = Instance.new('Frame')
	Tile.AnchorPoint = Vector2.new(0.5, 0.5)
	Tile.BackgroundTransparency = 1
	Tile.Position = UDim2.new(TilePosition.Position.X.Scale, 0, 0, 0)
	Tile.Size = UDim2.new(0.2, 0, 1, 0)

	if tileDirection == 'Left' then
		Text.Text = '<'
	elseif tileDirection == 'Up' then
		Text.Text = '<'
		Text.Rotation = 90
	elseif tileDirection == 'Right' then
		Text.Text = '>'
	elseif tileDirection == 'Down' then
		Text.Text = '>'
		Text.Rotation = 90
	end
			
	Tile.Parent = Scrolling
	
	UserInputService.InputBegan:Connect(function(input, GPE)
		if GPE then return end
		
		if not Tile then return end
		
		if input.KeyCode == Enum.KeyCode.Left then
			if tileDirection == 'Left' then
if Tile.Position.Y.Scale > TilePosition.Position.Y.Scale + 0.1 then
					print('Too late')
				elseif Tile.Position.Y.Scale < TilePosition.Position.Y.Scale - 0.1 then
					print('Too early')
				end
            end
        end
    end)
end

for i = 1, 10 do
	local RandomTileDirection = TileDirections[math.random(1, #TileDirections)]
	
	createTile(RandomTileDirection)
	wait(1)
end

As more Tiles pop up on screen, each click results to all of them. I only want to check for the bottom sequence

I’m confused on what you are trying to do. Can you elaborate on that a bit?

1 Like

I’m not completely sure what you’re trying to do, but it sounds like you want tiles to fall and the user to press a key once it reaches a certain point. If that’s what you’re trying to do, you’ll have to keep track of the frames and which one is the oldest. There are lots of ways to do this, but one way would be to store all frames in an array. Then, when one goes off-screen or is destroyed, you could remove it from the table. Just be careful that you get rid of old indexes properly. As an example of how this might look, here’s some code:

local tiles = {}

local function createTile(tileDir)
	-- create the tile, whatever
	local tileIndex = #tiles + 1
	tiles[tileIndex] = {Tile, tileDir}
	-- be sure to remove this from table when the tile is destroyed
	-- if it's always the 1st tile being removed then just:
	-- `table.remove(tiles, 1)` when destroying a tile should work.

	Tile.Parent = Scrolling
end

local function destroyTile(Tile)
	if tiles[1][1] == Tile then
		table.remove(tiles, 1)
		Tile:Destroy()
	else
		error([[pretty sure you only want the 
most recent tile removed but if not it's a lot 
more complicated]])
	end
end

UserInputService.InputBegan:Connect(function(input, GPE)
	local currTile = tiles[1] -- the current lowest tile (or it should be at least) 
	if input.KeyCode == Enum.KeyCode[currTile[2]] then
		-- if it's the right key do something
		-- maybe destroyTile(currTile[1])
	else
		-- wrong key
	end
end)
1 Like

It seems to set the ‘newest’ tile as the oldest. I want the oldest tile removed. So the first tile created is number one, second tile is the second, etc. And it should remove the oldest tile first

I think you should setup a counter for this, so you could know which is old tile and which is new tile. This is my suggestion, I would do it with an IntValue. Then when the program will know which tile is old you should only check for the existance and destroy them.