Heyyo scripters! I am a beginner scripter, and was trying to fire a remote event from a script to a localscript.
I got an error on the normal script, here’s the code on there:
local event = ReplicatedStorage.AddText
script.Parent.Touched:Connect(function(hit, plr)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
h.Health = h.Health - 0.5
event:FireClient(plr,"hi")
end
end)
Here was my error:
FireClient: player argument must be a Player object
I searched the Internet and DevForum, but got no results. If you can, please respond and provide help
You shouldn’t use WaitForChild in this case, especially without a time out (the second parameter). Because it’s not super important to have the humanoid, it’s run from a server script, and because lots of parts can hit the script’s parent, using WaitForChild isn’t a good choice.
The OP’s code is fine; FindFirstChild with an existence check is the best in this case.
Edit:
With explanations
Because it’s not super important to have the humanoid
(nothing will break if the character doesn’t have a humanoid, it should always have one on the server. Random parts don’t),
it’s run from a server script
(on the client, there is a chance that things will not have replicated yet, so WaitForChild is sometimes a good choice if you know something is a character or it’s important/only happens once)),
and because lots of parts can hit the script’s parent
(When you call WaitForChild, the system waits for a child. Without a time out, it continues to do this until the instance the function was called on is destroyed. In this case, this includes any part that hits the script’s parent),