Creating a sequence

I’ve seen an open sourced copy of RoBeats code, but that’s way too advanced for what I am trying to accomplish. I just want a simplistic sequence style game. My code attempt below + open file so you can see the UI lay out.

local UserInputService = game:GetService('UserInputService')

local UI = script.Parent

local Bottom = UI:WaitForChild('Bottom')
local Scrolling = UI:WaitForChild('Scrolling')

local TileDirections = {'Left', 'Up', 'Right', 'Down'}

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)
	
	local Text = Instance.new('TextLabel')
	Text.AnchorPoint = Vector2.new(0.5, 0.5)
	Text.BackgroundTransparency = 1
	Text.Position = UDim2.new(0.5, 0, 0.5, 0)
	Text.Size = UDim2.new(1, 0, 1, 0)
	Text.Font = Enum.Font.GothamBlack
	Text.TextColor3 = Color3.fromRGB(255, 255, 255)
	Text.TextScaled = true
	
	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
	
	local AspectRatio = Instance.new('UIAspectRatioConstraint')
	
	AspectRatio.Parent = Tile
	
	Text.Parent = Tile
	
	Tile.Parent = Scrolling
	
	spawn(function()		
		Tile:TweenPosition(UDim2.new(TilePosition.Position.X.Scale, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 2, true)
		wait(2)
		Tile:Destroy()
	end)
	
	UserInputService.InputBegan:Connect(function(input, GPE)
		if GPE then return end
		
		if input.KeyCode == Enum.KeyCode.Left then
			if tileDirection == 'Left' then
				if Tile.Position == TilePosition.Position then
					Tile:Destroy()
				end
			end
		end
		
		if input.KeyCode == Enum.KeyCode.Up then
			if tileDirection == 'Up' then
				if Tile.Position == TilePosition.Position then
					print('Correct timing')
					Tile:Destroy()
				end
			end
		end
		
		if input.KeyCode == Enum.KeyCode.Right then
			if tileDirection == 'Right' then
				if Tile.Position == TilePosition.Position then
					print('Correct timing')
					Tile:Destroy()
				end
			end
		end
		
		if input.KeyCode == Enum.KeyCode.Down then
			if tileDirection == 'Down' then
				if Tile.Position == TilePosition.Position then
					print('Correct timing')
					Tile:Destroy()
				end
			end
		end
	end)
end

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

Problem I have is checking if they’ve pressed the right key at the right time, since it’s checking for the static tile and the moving tile to be in the exact same spot. Is there a way to check for like levels of closeness? So like ± 0.025 would be perfect, ± 0.05 would be Great, +0.075 would be Ok, and anything other ± 0.1 would be a miss

Grab a copy here to try out
Sequence Test.rbxl (23.5 KB)

2 Likes

You’ll have to somehow compute the distance between two GuiObjects. The maths for doing so is just the pythagorean theorem. Here’s one way:

function guiDistance( guiObject1, guiObject2 )
	return math.sqrt(
		(guiObject1.Position.X.Scale) * (guiObject2.Position.X.Scale) + 
		(guiObject1.Position.Y.Scale) * (guiObject2.Position.Y.Scale)
	)
end

You can then compare the distance to some threshold you choose for each level of closeness.

3 Likes

In addition to @ThanksRoBama’s answer, for determining the level of ‘closeness’ you can use a simple if statement like so:

local GuiDistance = -- should be a positive value (if not, just *-1)
if GuiDistance < 0.025 then
    print("Perfect!")
elseif GuiDistance < 0.05 then
    print("Great")
elseif GuiDistance < 0.075 then
    print("Okay")
else
    print("Missed!")

Hope this helps!

2 Likes