How to Implement a Wait for Gui Button Press?

Title is pretty self-explanatory. I’ve tried a few different things but this is the closest I could come up with. There are 5 ImageButtons parented to deck. This function needs to wait for and return the ImageButton the player selects. I seem to lack understanding of how Wait() is used with input objects. I had some luck with UIS and getting the gui button at the position MB1 is clicked, but there’s got to be a way to simply wait for button activation.

local function WaitForInput(buttonPressed)
	print("Waiting for selected slot")
	for i, v in ipairs(deck:GetChildren()) do
		buttonPressed = v.Activated:Wait()	
	end
	return buttonPressed
end
2 Likes

I think you need to set up a connection to each button, the connection will wait until its clicked/tapped.

v.Activated:Connect(function()
--do  something when button pressed here
end)

look at the example at the bottom of this page:
https://developer.roblox.com/en-us/api-reference/class/ImageButton

So the problem is that the rest of the code already consists of what is essentially that. To simplify, the player presses a button, then it needs to wait for another button to be pressed, and that button needs to be returned.

This section of code is what calls the function in question

for i, button in ipairs(collection:GetChildren()) do
	button.UseRequest.Activated:Connect(function()
		local selectedSlot = WaitForInput()
		print(selectedSlot)
		selectedSlot.Image = button.Image
		selectedSlot.DiceInSlot.Value = button.Name
	end)
end

The MouseButton1Click event which is shared by various Gui button instances is fired whenever the button is clicked.

Here’s an example script which should help set you in the right direction.

local button = script.Parent
local isClicked = false

button.MouseButton1Click:Connect(function()
	isClicked = true
end)

repeat
	task.wait()
until isClicked
--do other code
4 Likes

:open_mouth: I had no idea that was the case. I thought .Activated was the only way to detect if a button is pressed. But what about other platforms like mobile? Does a tap still register as MouseButton1Click?

I take that back. I see now I should’ve gone back to the basics and looked at the events of ImageButton on the api. ImageButton | Roblox Creator Documentation

TouchTap is the equivalent for touch screen devices.

https://developer.roblox.com/en-us/api-reference/event/GuiObject/TouchTap

I noticed InputBegan would be the universal event for all platforms right?

So after some trial and error this is the only way I could make this work. Works without issue in this case anyways.

Loop that calls function

for i, button in ipairs(collection:GetChildren()) do
	button.UseRequest.Activated:Connect(function()
		local selectedSlot = WaitForInput()
		print(selectedSlot)
		selectedSlot.Image = button.Image
		selectedSlot.DiceInSlot.Value = button.Name
	end)
end

Function

local function WaitForInput(buttonPressed)
	local slot1 = deck:WaitForChild("1")
	local slot2 = deck:WaitForChild("2")
	local slot3 = deck:WaitForChild("3")
	local slot4 = deck:WaitForChild("4")
	local slot5 = deck:WaitForChild("5") 
	local done = false
	
	while not done do
		wait(1)
		slot1.Activated:Connect(function()
			buttonPressed = slot1
		end)
		slot2.Activated:Connect(function()
			buttonPressed = slot2
		end)
		slot3.Activated:Connect(function()
			buttonPressed = slot3
		end)
		slot4.Activated:Connect(function()
			buttonPressed = slot4
		end)
		slot5.Activated:Connect(function()
			buttonPressed = slot5
		end)
		if buttonPressed ~= nil then
			done = true
		end
	end	
	return buttonPressed	
end
2 Likes