How to get player for FireClient() from a server script?

For some context, I am attempting to create a playable drum set (linking for further context if needed) and to do this I am using a RemoteEvent.

Here is where my issue appears.
I am trying to use a RemoteEvent going from server to client. My main problem is for FireClient() you need to have an argument that is a player. Here is my attempt at this.

--[[ 
I am trying to get the player who is currently sitting in a seat
The name of my RemoteEvent is "drumEvent"
This is a server script within the seat
]]--

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	
	local playerInSeat = seat.Occupant.Parent
	print(playerInSeat)
	
	local player = game.Players:FindFirstChild(playerInSeat)
	print(player)
	
	game.ReplicatedStorage.drumEvent:FireClient(player)
	print("Pinging to client")

end)

Now the problem is trying to collect the user from this way fails as local playerInSeat = seat.Occupant.Parent collects it as an “instance” instead of a string. I am not really sure where to go from here and what I should be doing instead!

I could also very possibly be misunderstanding how to use FireClient and be doing this all completely incorrectly

I’m not sure if this is important but this is the client script that will be receiving

game.ReplicatedStorage.drumEvent.OnClientEvent:Connect(function()
	
	print("Client Firing")
	
end)

Seat.Occupant returns a Humanoid. You can use https://create.roblox.com/docs/reference/engine/classes/Players#GetPlayerFromCharacter to get the player, so it would be Players:GetPlayerFromCharacter(Occupant.Parent) which returns a player which you can the use in :FireClient.

The problem your code has is that local player = game.Players:FindFirstChild(playerInSeat) tries to find the Humanoids Parent which is the character inside of game.Players.

2 Likes

Got this one too in the last question..
This is the a bit tricky part I was talking about. The first parameter passed will always turn out to be the player. The rule to keep in mind is that first parameter client or server.

remote:FireServer(player, “Hello”)

omg, i mustve missed it, sorry!

No, you asked here the other way around, so here is that..
remote:FireClient(player, code, stuff) player is always first.
remote:FireClient(code, player, stuff) this would make code be the player.
I actually tried to avoid this in my last script, in the other thread.

3 Likes

The main issue is that FireClient expects a Player object, but seat.Occupant is a Humanoid. You were trying to use seat.Occupant.Parent and then FindFirstChild on that, but that doesn’t give you the player, it gives you the character model.

What I recommend doing is this:

  1. When you have a Humanoid (seat.Occupant), you can get its character model via .Parent.
  2. Then, you can find the Player associated with that character using Players:GetPlayerFromCharacter(character).

local Players = game:GetService(“Players”)
local seat = script.Parent

like this:

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local humanoid = seat.Occupant
    if humanoid then
        local character = humanoid.Parent
        local player = Players:GetPlayerFromCharacter(character)
        if player then
            game.ReplicatedStorage.drumEvent:FireClient(player)
            print("Pinging to client")
        end
    end
end)

Hopefully this helps!

Remember to mark the answer as the “solution” to close the thread.

2 Likes

right now you’re checking if the player’s character is in game.Players when you should be checking

local player = game.Players:FindFirstChild(playerInSeat.Name)

hope this helped!