Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

RemoteEvent won't use passed argument and will just print player name?

Asked by 8 years ago
Edited 8 years ago

I don't get this. Take a look:

I have a localscript named "Test" here it is:

local modules = game.Workspace["Module(s)"];
local chathandler = require(modules.ChatHandler);

local msg = ("I love icecream.");

chathandler.test(game.Players.LocalPlayer,msg);

I have a modulescript, here's how it looks in relation to that code:

function chathandler.test(player,msg)
    chatAdd:FireServer(player,msg);
end

Now here's the RemoteEvent which should print the msg correctly:

chatAdd.OnServerEvent:connect(function(player,msg)
    print(msg);
end)

What's going on? Why does it print the Player's name but not the msg itself? Is it not passed correctly?

EDIT (Update, just uses msg this time): Still doesn't work.

New localscript:

local modules = game.Workspace["Module(s)"];
local chathandler = require(modules.ChatHandler);

local msg = ("I love icecream.");

chathandler.test(msg);

Modulescript:

function chathandler.test(msg)
    chatAdd:FireServer(msg);
end

RemoteEvent:

chatAdd.OnServerEvent:connect(function(msg)
    print(msg);
end)

2 answers

Log in to vote
0
Answered by 8 years ago

This has to do with something a little funky about RemoteEvents. When you cal :FireServer() on a RemoteEvent from a client, it automatically passes the player that called it as the first argument.

So that means this:

remoteEvent:FireServer()

would need this on the server to work properly:

remoteEvent.onServerEvent:connect(function(plr)
    --print(plr)
end)

You can pass other arguments through the RemoteEvent, but the player that called the server will always be the first argument received.

0
Simple, concise, and easy to understand. +1 thanks to the above too. Arithmeticity 167 — 8y
Ad
Log in to vote
1
Answered by
Master_JJ 229 Moderation Voter
8 years ago

I think it is because of your local script.

When using remote events in the local script, the first argument is automatically defined as player, so the first thing you send to a remote event in the local script will be looked as the second argument, so it thinks the msg is the player.

Answer this question