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

Server Script is Printing the LocalPlayer's Name?

Asked by
H26O 44
5 years ago

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)
0
What SHOULD it be printing? DinozCreates 1070 — 5y
0
Don't forget to hit that accept answer button if my solution helps. User#24403 69 — 5y
0
You're sending the LocalPlayer's character through FireServer which the OnServerEvent will then have two parameters given (Assumed by reading this). The first is the player communicating and second would be the character model you sent. xPolarium 1388 — 5y
0
Ghosted yeet xPolarium 1388 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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.

2
Lame. DinozCreates 1070 — 5y
0
The reason it uses script.Parent.Parent is because its stored inside a tool for a target based attack H26O 44 — 5y
0
Oh my bad. I assumed it was in a GUI or something. User#24403 69 — 5y
0
-rep has no friends Gey4Jesus69 2705 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

The first argument passed by OnServerEvent will by default be the player.

https://developer.roblox.com/api-reference/event/RemoteEvent/OnServerEvent

0
oof SteamG00B 1633 — 5y

Answer this question