Well the title states that this is a request, but it's not, i'm having trouble finding a characters humanoid and damaging it on hit, This is my script, its in a serverscript in a tool, Whats wrong?
local damage = script.Parent.Configuration.Damage.Value script.Parent.Part.Touched:Connect(function() local hum = script.Parent.Part:FindFirstChildOfClass("Humanoid") hum.Health = - damage end)
Thanks for reading,
Well, first of all, (almost) every :Connect(function() returns a argument in "function(arg1)" (which of, arg1 is the argument it returns.). In this case, the :Connect(function(arg1) returns the part being hit. (:Connect(function(partBeingHit)). So, knowing that, you can do:
local damage = script.Parent.Configuration.Damage.Value script.Parent.Part.Touched:Connect(function(partBeingHit) local hum = partBeingHit.Parent:FindFirstChild("Humanoid") --most of the cases, the partBeingHit is a leg, a arm, the torso or the head. These limbs are inside a model (thats the player in some cases) and, inside the model, theres the Humanoid. Knowing that, the limbs' parent will be the player model, and, if theres a humanoid in it (some cases there arent), it will subtract the health. if hum then hum.Health = - damage end end)
But wait, theres a problem here: A error occours in line 6. Well, the maths here are wroten wrong.
if hum then hum.Health = - damage --wroten wrong end
the correct way would be:
if hum then hum.Health = hum.Health - damage --sets the hum.Health as hum.Health and subtracts it by the damage end
that works. heres the fixed version, without all the explaining:
local damage = script.Parent.Configuration.Damage.Value script.Parent.Part.Touched:Connect(function(partBeingHit) local hum = partBeingHit.Parent:FindFirstChild("Humanoid") if hum then hum.Health = hum.Health - damage end end)
Hope that helped.