Working with remotes in a game, why does this script print the LocalPlayer's name?
1 | -- Local Script |
2 |
3 | if t.Parent:FindFirstChild( "Humanoid" ) then |
4 | local cTarget = t.Parent |
5 | game:GetService( "UserInputService" ).InputBegan:connect( function (inputObject, gameProcessedEvent) |
6 | if inputObject.KeyCode = = Enum.KeyCode.Q then |
7 | script [ "RemoteEvent" ] :FireServer(cTarget) -- Sends cTarget to the ServerScript |
8 | print (cTarget) -- Part works and prints the correct value |
9 | end |
1 | -- Server Script |
2 |
3 | script.Parent.Parent [ "RemoteEvent" ] .OnServerEvent:connect( function (cTarget) |
4 | print (cTarget) -- Prints LocalPlayer's name for some reason |
5 | end ) |
It prints the player's name because the first argument to your callback function connected to OnServerEvent
is always the player who fired the remote event. If the player serves no purpose in your situation then just use a placeholder variable.
1 | script.Parent.Parent [ "RemoteEvent" ] .OnServerEvent:Connect( function (_, cTarget) |
2 | print (cTarget) |
3 | end ) |
This should properly print what you sent.
Also I don't understand why the remote event is in script.Parent.Parent
. It should be in somewhere like replicated storage.
The first argument passed by OnServerEvent will by default be the player.
https://developer.roblox.com/api-reference/event/RemoteEvent/OnServerEvent