luarobloxluauroblox-studio

Passing arguments through a RemoteEvent in Roblox


I'm currently trying to make a message system in Roblox. The server should be able to send a message via a RemoteEvent, and the client should be able to accept it by displaying the message on a GUI TextBox.

However, I can't seem to pass arguments to onDisplayMessage() from the displayMessageEvent. When I fire the RemoteEvent, nothing happens.

This is the server script thus far:

local displayMessageEvent = Instance.new("RemoteEvent")
displayMessageEvent.Name = "DisplayMessageEvent"
displayMessageEvent.Parent = ReplicatedStorage
displayMessageEvent:FireAllClients("Hello, World!")

And this is the client script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local displayMessageEvent = ReplicatedStorage:WaitForChild("DisplayMessageEvent")

local function onDisplayMessage(message)
    local player = Players.LocalPlayer
    local playerGui = player:WaitForChild("PlayerGui")
    local screenGui = playerGui:WaitForChild("ScreenGui")
    local textLabel = screenGui:WaitForChild("TextLabel")

    textLabel.Text = message
end

displayMessageEvent.OnClientEvent:Connect(onDisplayMessage())

I tried replacing the last line with the following, with similar results:

displayMessageEvent.OnClientEvent:Connect(onDisplayMessage(displayMessageEvent.event))

How can I get the client to handle the RemoteEvent correctly?


Solution

  • You're a lot closer than you might think! It comes down to a very subtle error in your last line, which should instead read:

    displayMessageEvent.OnClientEvent:Connect(onDisplayMessage)
    

    Notice that I removed the inner parentheses after onDisplayMessage.

    What you got right

    Your initial intuition was correct. When you bind a function to an event with Connect, you don't have to do much work with arguments in the moment. You just have to make sure to call FireAllClients with the same number of arguments your connected function is expecting whenever you use it. In other words, assuming you connected the function to the event properly, your code does exactly what you wanted it to.

    Why it didn't work

    The problem is how you're connecting your function to the event. Connect expects a function (also known as a callback in this context), but you're actually not giving it one since onDisplayMessage() isn't a function. More specifically, there's a difference between onDisplayMessage and onDisplayMessage() - the first is a function, and the second is a function call.

    So when Roblox sees this line:

    displayMessageEvent.OnClientEvent:Connect(onDisplayMessage())
    

    It doesn't bind the function onDisplayMessage to the event. Instead, it calls onDisplayMessage with no arguments, then binds its return value to the event.1

    Removing the parentheses ensures that you're binding the function itself, not calling the function and binding the value it returns.


    1. This is probably throwing a quiet error since you're calling the function with the wrong number of arguments, but I honestly don't know.