How can I get the player from a character through a remote event?

Hi DevForum,

I’m making a combat system and want to get the player from the enemy’s character.

I fired the remote event through code like this:

local data = {
	["Character"] = character,
	["TargetCharacter"] = target -- Note that this is the target's CHARACTER
}
			
CombatEvent:FireServer(data)

Now when I receive this remote event on the ServerScript, and try to fire the client to the target’s player, I get an error stating “FireClient: player argument must be a Player object”.

I have tried using multiple methods, such as :GetPlayerFromCharacter(), but all gave the same error.

Help is appreciated, thanks.

You can try sending the user id instead and using

local player = game:GetService('Players'):GetPlayerByUserId(UserId)

Thanks, I appreciate the reply,

How would I get the UserId from the character though?

Something to understand about RemoteEvents and RemoteFunctions is that they prepend a Player argument when the server is receiving, and you have to prepend a Player argument yourself when your server code is sending information to individual clients (unless you’re using RemoteEvent::FireAllClients).

image

For example, here’s some client code that sends the local player’s camera position to the server:

local cameraPos = workspace.CurrentCamera.CFrame.Position
Remote:FireServer(cameraPos)

The server will receive it like this:

Remote.OnServerEvent:Connect(function(Player, cameraPos)

end)

The Player who fired that remote is automatically added there by Roblox.


Similarly, if I wanted to send some save data to an individual client, my server code would look something like this:

local Player = game.Players.GFink
local inventory = InventoryList[Player]
Remote:FireClient(Player, inventory)

The client code will receive it like this:

Remote.OnClientEvent:Connect(function(inventory)

end)

where Roblox automatically removes the Player argument, since the client doesn’t need it. The client can just use game.Players.LocalPlayer.

On a sidenote, I’ve never found a need to send data to any particular client from the server. If I need to give data to a single client, I always found it made more sense to have the client ask for that information first using a RemoteFunction, where instead of the server having to do RemoteFunction:InvokeClient(player, ...) you can just do return ... from the server side part of the function. Here’s a relevant sequence of posts I wrote if you’re curious

*Well that was stupid I wasn’t thinking, anyway:

Players:GetPlayerFromCharacter(character) should always give back the player unless the argument given isn’t a character. Considering this isn’t working try printing out target, checking the parent etc. to see if it actually is a character

Thanks, it was also me who made a small mistake, I was attacking a dummy and trying to get the player from it, but this works, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.