The function is not returning properly

Im making a module to make a invite system, and i need to return if the player accepts or not the invite, if the player dont reply the invite expire. But its only returning expire, i need to know how to return the other options. Here is the code:

module.Request = function(requestedPlayer, playerWhoRequested)
	local Frame = game.ReplicatedStorage.Frames.RequestFrame:Clone()
	local AcceptClick
	local RejectClick

	Frame.Message.Text = playerWhoRequested.Name .. " Has invited you to fight with them"
	Frame.PlayerWhoRequested.Value = playerWhoRequested.Name	
	Frame.Parent = requestedPlayer.PlayerGui.Boxingui

	AcceptClick = acceptClickRemote.OnServerEvent:Connect(function(plr)
		AcceptClick:Disconnect()
		Frame:Destroy()
		print("Accepted")
		return "Accepted"
	end)

	RejectClick = rejectClickRemote.OnServerEvent:Connect(function(plr)
		RejectClick:Disconnect()
		print("Rejected")
		Frame:Destroy()
		return "Rejected"
	end)

	wait(15)

	if RejectClick and AcceptClick then
		RejectClick:Disconnect()
		AcceptClick:Disconnect()
	end

	Frame:Destroy()
	return "Expired"
end
2 Likes

Your function is returning properly, your misconception lies in where the value gets returned to. Accepted and Rejected are getting returned to their respective functions that you’re connecting to a signal (signals do not expect, need or use return values) while Expired is being returned to Request. You’ll need to write this a different way.

A quick way to resolve this is to declare a variable with no value holding the decision of the request and replace your 15-second wait with a repeat loop that terminates itself when the variable becomes non-nil or the counter reaches 15 seconds. If a value hasn’t been set after the natural timeout, set it to expire and then return the decision. AcceptClick and RejectClick should set the value respectively.

module.Request = function()
    local Decision
    local Ticks = 0

    -- Obviously don't write it like this, this is not valid
    AcceptClicked(Decision = "Accepted")
    RejectClicked(Decision = "Rejected")

    while (not Decision) or (Ticks < 15) do
        Ticks += 1
        -- Helps terminate quicker if Decision is set to non-nil during loop
        if not Decision then wait(1) end
    end

    if not Decision then
        Decision = "Expired"
    end

    return Decision
end
2 Likes