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!
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)