How can I allow the user to touch the buttons in any order?

So I am just making a system where you have to click the keybind on the screen and it disappears and you get 1 hit.

It does work but however you need to hit them all in the order of they were created, what would I need to alter in order to make the user be able to click them in any order.

No errors.

Local Script:

local Keys = {
	[1] = {KeyCode = Enum.KeyCode.F, Display = "F"},
	[2] = {KeyCode = Enum.KeyCode.L, Display = "L"},
	[3] = {KeyCode = Enum.KeyCode.G, Display = "G"},
	[4] = {KeyCode = Enum.KeyCode.N, Display = "N"}
}

local BackgroundColors  = {Color3.fromRGB(85, 170, 255), Color3.fromRGB(85, 255, 127),Color3.fromRGB(255, 89, 89),Color3.fromRGB(255, 89, 89),Color3.fromRGB(245, 205, 48)}
local CurrentKeys = {}

local UIS = game:GetService("UserInputService")

local Template = script.Parent.Template

local Playing = true
local Hits = 0

function genPos()
	local r = Random.new()
	return UDim2.new(r:NextNumber(0,1),0,r:NextNumber(0,1),0)
end

function getRandomKey()
	local random = math.random(1, #Keys)
	local Selected = Keys[random]
	return Selected
end

function getRandomColour()
	local random = math.random(1, #BackgroundColors)
	local Selected = BackgroundColors[random]
	return Selected
end

function getIndex(a)
	for i, v in pairs(CurrentKeys) do
		if v == a then
			return i
		end
	end
end

function FadeOut(Button, a)
	Button.Radius:TweenSize(UDim2.new(0,100,0,100), Enum.EasingDirection.In, Enum.EasingStyle.Quart, .2)
	wait(.3)
	Button:Destroy()
	local Index = getIndex(a)
	table.remove(CurrentKeys, Index)
end

function getObject(Value)
	for _, a in pairs(CurrentKeys) do
		print(a.KeyCode, Value)
		if a.Keycode == Value then
			return a
		end
		return false
	end
end

if UIS.KeyboardEnabled == true then
	spawn(function()
	while Playing == true do
		local Button = Template:Clone()
		Button.Parent = script.Parent.Screen
		local Key = getRandomKey()
		Button.Display.Text = Key.Display
		Button.Name = Key.Display
		Button.BackgroundColor3 = getRandomColour()
		Button.Position = genPos()
		Button.Radius.BackgroundColor3 = Button.BackgroundColor3
		CurrentKeys[#CurrentKeys+1] = {Keycode = Key.KeyCode, Display = Key.Display}
		print(CurrentKeys)
		Button.Visible = true
		wait(5)
		end
		end)
else
	print("mobile")
end

UIS.InputBegan:Connect(function(K, GP)
	if GP then return end
	local A = getObject(K.KeyCode)
	if A then
		local Obj = script.Parent.Screen:FindFirstChild(A.Display)
		FadeOut(Obj, A)
		Hits += 1
		if Hits == 10 then
			Playing = false
			end
	end
end)

Video of it: https://gyazo.com/6c08a7f5a53e57e8d5973f052ae94acd

I think this is the function that causes it:

function getObject(Value)
	for _, a in pairs(CurrentKeys) do
		print(a.KeyCode, Value)
		if a.Keycode == Value then
			return a
		end
		return false -- This means that after one cycle, the loop will end
	end
end

I think putting the “return false” out of the loop scope could fix it, as:

function getObject(Value)
	for _, a in pairs(CurrentKeys) do
		print(a.KeyCode, Value)
		if a.Keycode == Value then
			return a
		end
	end
    return false -- We looped through all but couldn't find it, time to return false
end

Alrighty, I’ll give that a go!

Yup, it works! Thank you very much!

1 Like