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

Help with receiving remoteEvents needed!?

local replicatedstorage = game:GetService("ReplicatedStorage")
local remotefunction = replicatedstorage:WaitForChild("RemoteEvent")

local player = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
  remotefunction:FireServer(player)
end)

I have a tool with a localscript containing the above. It is meant to fire the local player name to the server.

This is all I have for the server script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local remotefunction = replicatedstorage:WaitForChild("RemoteEvent")

I need the server to receive the local player name from the remoteEvent. No tutorials were good at explaining so I came here. Help please!

1 answer

✓ Accepted answer

Here we can make use of the event OnServerEvent [1]. It is used like so:

remote_event.OnServerEvent:Connect(function(p, arg1, arg2, arg3) -- etc.

end)

where arg1, arg2 (and so on) are the arguments which are passed to FireServer(arg1, arg2) in a localscript.

In your case, an appropriate receiver for the RemoteEvent would look like this:

remote_event.OnServerEvent:Connect(function(p, player) -- etc.
	print(player_arg.Name)
end)

What's more, we can make use of the engine's capability to automatically fill in the correct firing player. You may have noticed that I glossed over the p argument above. It turns out it's exactly what we need: a trusted way to tell which Player fired the RemoteEvent!

Put this in a server script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local remoteevent = replicatedstorage:WaitForChild("RemoteEvent")

remoteevent.OnServerEvent:Connect(function(p)
	print(p.Name)
end)

and put this in a local script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local remotefunction = replicatedstorage:WaitForChild("RemoteEvent")

script.Parent.Activated:Connect(function()
  remotefunction:FireServer() -- player argument will be automatically filled in by the server.
end)

[1]: Roblox DevHub Reference: OnServerEvent

4 comments

0

What is the p in (p, player) for?

0

@6zk8 p stands for the LocalPlayer, the other arguments after the first one are tuple arguments. @gskw why is your second argument player since the first argument already passes the LocalPlayer tho?

0

Nvm, thanks!

0

@guest_2018 I added in the second player argument to illustrate how RemoteEvents would work with OP's initial setup, where FireServer() sends an additional player agument.

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