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

Help with receiving remoteEvents needed!?

Asked by
6zk8 95
4 years ago
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

Log in to vote
1
Answered by
gskw 1046 Moderation Voter
4 years ago
Edited 4 years ago

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

0
What is the p in (p, player) for? 6zk8 95 — 4y
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? guest_20I8 266 — 4y
0
Nvm, thanks! 6zk8 95 — 4y
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. gskw 1046 — 4y
Ad

Answer this question