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