Basic Script Help

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 :slight_smile:

1 Like

Try getting the player by using

local Player = game.Players:GetPlayerFromCharacter(h.Parent)

Try and pass that through instead

3 Likes

“plr” is not an argument for a player. You have to catch the player by hit.Parent. Like:

local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get the player for some part of his character that touched the part
6 Likes

instead of findfirstchild do :WaitForChild("Humanoid")

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

using WaitForChild isn’t a good choice.