Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Client instantly returns server with false every time?

Basically I invoke the client on the server to show a prompt GUI with two buttons "Yes" and "No". I want to make it return either false or true to what they pick, but whenever I put the return true and return false in their respective MouseButton1Click functions, the ClientInvoke function instantly returns with false every time no matter if you pick true or false or if you even pick any of them. I'm pretty new to RemoteFunctions so any help would be appreciated.

events = game:GetService("ReplicatedStorage").GameEvents.BankRobbery
promptcontainer = script.Parent:WaitForChild("PromptContainer")

events.PromptBankRobbery.OnClientInvoke = function()
    promptcontainer.Visible = true
    promptcontainer.Yes.MouseButton1Click:Connect(function()
        promptcontainer.Visible = false
        return true
    end)
    promptcontainer.No.MouseButton1Click:Connect(function()
        promptcontainer.Visible = false
        return false
    end)
end

1 answer

✓ Accepted answer

It's actually most likely returning nil, not false. This is because your return statements return from your anonymous functions, but not from the OnClientInvoke function. To fix this, you must find a way to tell OnClientInvoke what to return.

Furthermore, you should disconnect your connections once you are done with them, otherwise you may run into problems.

Here's one way to solve this problem:

events = game:GetService("ReplicatedStorage").GameEvents.BankRobbery
promptcontainer = script.Parent:WaitForChild("PromptContainer")

events.PromptBankRobbery.OnClientInvoke = function()
    promptcontainer.Visible = true
    local returnValue -- Declare a return value variable.
    local yesConnection, noConnection -- Declare our connection variables.
    yesConnection = promptcontainer.Yes.MouseButton1Click:Connect(function()
        -- Disconnect our connectionis now that a button has been clicked.
        yesConnection:disconnect()
        noConnection:disconnect()
        promptcontainer.Visible = false
        returnValue = true -- Set our return value for OnClientInvoke
    end)
    noConnection = promptcontainer.No.MouseButton1Click:Connect(function()
        yesConnection:disconnect()
        noConnection:disconnect()
        promptcontainer.Visible = false
        returnValue = false
    end)
    while returnValue == nil do wait() end -- Wait until a button is clicked.
    return returnValue -- Now actually return the value you want.
end

This discussion is preserved as it was. New answers, comments, and votes are closed.