Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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.

1local ReplicatedStorage = game:GetService("ReplicatedStorage")
2local Event = game.ReplicatedStorage:WaitForChild("Event")
3script.Parent.Touched:Connect(function(hit)
4    print("Hit")
5    if hit.Parent:FindFirstChild("Humanoid") then
6        print("Human Hit")
7        Event:FireClient(hit.Parent)
8    end
9end)
0
It probably from the hit.Parent part on line 7 theking66hayday 841 — 2y
0
Yes I said it. opgmltu 0 — 2y

2 answers

Log in to vote
1
Answered by 2 years ago

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().

01local Players = game:GetService("Players")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03local Event = ReplicatedStorage.Event
04 
05script.Parent.Touched:Connect(function(hit)
06    print("Hit")
07 
08    local character = hit.Parent
09    local humanoid = character:FindFirstChildOfClass("Humanoid")
10    if character and humanoid then
11        print("Human Hit")
12 
13        local player = Players:GetPlayerFromCharacter(character)
14        if player then
15            print("Player Hit")
16            Event:FireClient(player)
17        end
18    end
19end)
0
Beat me to it xInfinityBear 1777 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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().

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local Event = game.ReplicatedStorage:WaitForChild("Event")
03 
04script.Parent.Touched:Connect(function(hit)
05    print("Hit")
06    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
07    if Player then
08        Event:FireClient(Player)
09    end
10end)

Answer this question