Something is wrong with my script?

So I’ve made this script that once the game start you must click every frame in order, but it doesn’t seem to work it worked perfectly fine at the start but I can’t get it to how it worked. any help?

local frameOrder = { "TextButton1", "TextButton2", "TextButton3" } 
local currentIndex = 1
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local n1 = script.Parent.TextButton1
local n2 = script.Parent.TextButton2
local n3 = script.Parent.TextButton3

local newX = math.random(1, 9) / 10
local newY = math.random(1, 9) / 10


script.Parent.Ready.MouseButton1Click:Connect(function()
--	wait(4)
	
	print("run")
	n1.Visible = true
	n2.Visible = true
	n3.Visible = true
	
	n1.Position = UDim2.new(newX, 0, newY, 0)
	n2.Position = UDim2.new(newX, 0, newY, 0)
	n3.Position = UDim2.new(newX, 0, newY, 0)

	local function onClick(button)
		if button.Name == frameOrder[currentIndex] then
			button.Visible = false 
			currentIndex = currentIndex + 1 
			if currentIndex > #frameOrder then
				
				print("Complete")
				
			end
		end
	end
	
	for _, buttonName in ipairs(frameOrder) do
		local button = script.Parent:FindFirstChild(buttonName)
		if button then
			button.MouseButton1Click:Connect(function()
				onClick(button)
			end)
		end
	end
end)

3 Likes

You assigned math.random(1, 9)/10 as a variable for both newX and newY so all three buttons’ positions will remain similar.

n1.Position = UDim2.new((math.random(1, 9) / 10), 0, (math.random(1, 9) / 10), 0)
n2.Position = UDim2.new((math.random(1, 9) / 10), 0, (math.random(1, 9) / 10), 0)
n3.Position = UDim2.new((math.random(1, 9) / 10), 0, (math.random(1, 9) / 10), 0)

This can also depend on the anchor point/size of each button.

Thank you so much, I’ve bent trying to figure out what I did wrong for such a long time debugging everything and it was right in front of my eyes

1 Like

No problem. Also:

I found out a better way to write this:

local function randomizeXY(button)	
	local xy = math.random(1, 9) / 10
	button.Position = UDim2.new(xy, 0, xy, 0)
end

-- This goes inside the "MouseButton1Click" function

randomizeXY(n1)
randomizeXY(n2)
randomizeXY(n3)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.