Working with remotes in a game, why does this script print the LocalPlayer's name?
-- Local Script if t.Parent:FindFirstChild("Humanoid") then local cTarget = t.Parent game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Q then script["RemoteEvent"]:FireServer(cTarget) -- Sends cTarget to the ServerScript print(cTarget) -- Part works and prints the correct value end
-- Server Script script.Parent.Parent["RemoteEvent"].OnServerEvent:connect(function(cTarget) print(cTarget) -- Prints LocalPlayer's name for some reason 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.
script.Parent.Parent["RemoteEvent"].OnServerEvent:Connect(function(_, cTarget) print(cTarget) 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