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
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
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
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?
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