Why is the result of :GetPlayerFromCharacter() not returning a "Player object?"

I am trying to fire a remote event to a specific client with two values when a player hits a part, However, when I try to fire the remote event to the player, it returns the error “FireClient: player argument must be a Player object” I tried seeing if the player was returing as nil but the player is found. Here is the script:

local Trigger = script.Parent
local textList = {
	"Test1", 
	"Another Test Message", 
	"A Third Test"
}
local num = 1

Trigger.Touched:Connect(function(hit)
	local Char = hit.Parent
	local plyr = game.Players:GetPlayerFromCharacter(Char)
	print(plyr)
	game.ReplicatedStorage.TextEvent:FireClient(plyr, textList, num)	
end)

I don’t usually use remote events although it seemed it would be easiest if I used one in this scenario.

To find the player by character, you must first have the character.

The character will always be a Model so try to find an ancestor who is one:

local Char = hit:FindFirstAncestorWhichIsA("Model")

So if the instance is in a Model then you will find it.

You need to check if the hit is a Character by checking for a Humanoid instance.
This way, we can verify if the parameter is a character and not just any BasePart that would trigger the signal.

local Trigger = script.Parent
local textList = {
	"Test1", 
	"Another Test Message", 
	"A Third Test"
}
local num = 1

Trigger.Touched:Connect(function(hit)
	if hit.Parent then
		local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
		
		if Humanoid then
			local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			
			if Player then
				game.ReplicatedStorage.TextEvent:FireClient(Player, textList, num)
			end
		end
	end
end)

Thanks. I forgot that all parts can activate it.

1 Like

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