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.
1 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
2 | local Event = game.ReplicatedStorage:WaitForChild( "Event" ) |
3 | script.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 |
9 | end ) |
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()
.
01 | local Players = game:GetService( "Players" ) |
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
03 | local Event = ReplicatedStorage.Event |
04 |
05 | script.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 |
19 | end ) |
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()
.
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local Event = game.ReplicatedStorage:WaitForChild( "Event" ) |
03 |
04 | script.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 |
10 | end ) |