Simple script for guess the order of the numbers game

I saw some other games have this small minigame for doing some action in the game and felt like replicating it. I am posting this here in hopes that it might help someone, as i have learned from devforum countless times myself. If you want to use it, go ahead.


local currentNumGuess = 1

local firstNumChosen
local secondNumChosen
local thirdNumChosen
local fourthNumChosen

local sequenceString

local testUI = script.Parent

local button1 = testUI.B1
local button2 = testUI.B2
local button3 = testUI.B3
local button4 = testUI.B4

function generateNum()
	local num
	repeat
		num = math.random(1, 4)
	until not numbersAlreadyGenerated[num]
	return num
end

function generateSequence()
	firstNumChosen = generateNum()
	numbersAlreadyGenerated[firstNumChosen] = true

	secondNumChosen = generateNum()
	numbersAlreadyGenerated[secondNumChosen] = true

	thirdNumChosen = generateNum()
	numbersAlreadyGenerated[thirdNumChosen] = true

	fourthNumChosen = generateNum()
	numbersAlreadyGenerated[fourthNumChosen] = true

	sequenceString = tostring(firstNumChosen) .. tostring(secondNumChosen) .. tostring(thirdNumChosen) .. tostring(fourthNumChosen)
	print(sequenceString)
end

generateSequence()

for i, v in pairs(testUI:GetChildren()) do
	if v:IsA("LocalScript") == false then
		v.MouseButton1Click:Connect(function()
			if v.Text == string.sub(sequenceString, currentNumGuess, currentNumGuess) then
				currentNumGuess += 1
				v.BackgroundColor3 = Color3.new(0.266667, 1, 0.101961)
				print("correct")
			else
				currentNumGuess = 1
				for i, v in pairs(testUI:GetChildren()) do
					if v:IsA("LocalScript") == false then
						v.BackgroundColor3 = Color3.new(1, 1, 1)
					end
				end
				print("fail")
			end
		end)
	end
end
2 Likes