How can I wait for this to return?

So basically I have this function.

local function displayPlayers()
	plrList.Visible = true
	for _, player in pairs(players:GetChildren()) do
		local tag = plrList:FindFirstChild('PLRTAG'):Clone()
		tag.Visible = true
		tag.TextButton.Text = player.Name
		tag.Parent = plrList
		
		tag.TextButton.Activated:Once(function()
			print('Player pressed!'..player.Name)
			plrList.Visible = false
			-- remove items in plrList
			for _, item in pairs(plrList:GetChildren()) do
				if item:IsA('Frame') then
					item:Destroy()
				end
			end
			-- send back player name
			return player
		end)
	end
	
	
end

as you can see in the script there is a return. This return comes once a player presses on a button.

Now where I call that script I need to make it so the script waits until the Value is returned.
Anyway to do this?

As long as you’re not doing task.spawn or making a coroutine it should yield until the function finishes.

WEll the function does finish. Its using .Activated. Which means its not going to cause a delay.

If you dont need the players to click the same button twice then you could do it like this

local function displayPlayers()
	plrList.Visible = true
	for _, player in pairs(players:GetChildren()) do
		local tag = plrList:FindFirstChild('PLRTAG'):Clone()
		tag.Visible = true
		tag.TextButton.Text = player.Name
		tag.Parent = plrList
		
		tag.TextButton.Activated:Wait()

		print('Player pressed!'..player.Name)
		plrList.Visible = false
		-- remove items in plrList
		for _, item in pairs(plrList:GetChildren()) do
			if item:IsA('Frame') then
				item:Destroy()
			end
		end
		-- send back player name
		return player
	end
	
	
end
1 Like

Use :Wait() instead and take everything out of the function,

Also I do not think waiting for something like this is a good Idea.

Instead of using .Activated directly, you could* use a single bindable event which is called when one of those buttons is pressed and use :Wait on it

something like:

local function displayPlayers()
	local playerSelected = Instance.new("BindableEvent")

	plrList.Visible = true
	for _, player in pairs(players:GetChildren()) do
		local tag = plrList:FindFirstChild('PLRTAG'):Clone()
		tag.Visible = true
		tag.TextButton.Text = player.Name
		tag.Parent = plrList

		tag.TextButton.Activated:Connect(function()
			print('Player pressed!'..player.Name)
			plrList.Visible = false
			for _, item in pairs(plrList:GetChildren()) do
				if item:IsA('Frame') then
					item:Destroy()
				end
			end
			-- signal that a player has been selected
			playerSelected:Fire(player.Name)
		end)
	end

	-- wait for a player to be selected
	local selectedPlayerName = playerSelected.Event:Wait()
	playerSelected:Destroy()

	-- return the selected player's name
	return selectedPlayerName
end

The reason I have it in a function is cause I am going to use it multiple times, anyhow I dont see how this could be a bad thing(Personal me) But I think what @syxnlucas said will work

If my solution did work please consider marking my reply as the solution :slightly_smiling_face:

2 Likes

I havent tried it yet and I keep forgetting. Ill try it out soon

1 Like