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 1 year 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.

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)
0
It probably from the hit.Parent part on line 7 theking66hayday 841 — 1y
0
Yes I said it. opgmltu 0 — 1y

2 answers

Log in to vote
1
Answered by 1 year 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().

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)
0
Beat me to it xInfinityBear 1777 — 1y
Ad
Log in to vote
0
Answered by 1 year ago
Edited 1 year 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().

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)

Answer this question