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

Why am I getting FireClient: player argument must be a Player object ?

Hello, I'am getting an error "Event:FireClient(hit.Parent)" I'm not sure why It doesn't works since it passes a player variable, I've tried changing it up but it doesn't seem to help.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = game.ReplicatedStorage:WaitForChild("Event")
script.Parent.Touched:Connect(function(hit)
	print("Hit")
	if hit.Parent:FindFirstChild("Humanoid") then
		print("Human Hit")
		Event:FireClient(hit.Parent)
	end
end)

2 comments

0

It probably from the hit.Parent part on line 7

0

Yes I said it.

2 answers

hit.Parent only returns the player's character if touched by a player. The player's character is a Model and not the Player object. To get the Player object from the character Model, use Players:GetPlayerFromCharacter().

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.Event

script.Parent.Touched:Connect(function(hit)
	print("Hit")
	
	local character = hit.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if character and humanoid then
		print("Human Hit")
		
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			print("Player Hit")
			Event:FireClient(player)
		end
	end
end)

1 comment

0

Beat me to it

The FireClient function requires the Player object, so it knows which client it will be firing to; since you put the player's character as the first argument, it will error (as it did) because it is not a player object.

You can get the character's player object by using Players:GetPlayerFromCharacter().

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = game.ReplicatedStorage:WaitForChild("Event")

script.Parent.Touched:Connect(function(hit)
	print("Hit")
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		Event:FireClient(Player)
	end
end)

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