Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Server Script is Printing the LocalPlayer's Name?

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)

4 comments

0

What SHOULD it be printing?

0

Don't forget to hit that accept answer button if my solution helps.

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.

0

Ghosted yeet

2 answers

✓ Accepted answer

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.

4 comments

2

Lame.

0

The reason it uses script.Parent.Parent is because its stored inside a tool for a target based attack

0

Oh my bad. I assumed it was in a GUI or something.

0

-rep has no friends

This discussion is preserved as it was. New answers, comments, and votes are closed.