Ball.Touched:connect(function(hit) local hum = hit.Parent.Parent:FindFirstChild("Humanoid") if hum == nil then hit.Health = hit.Health - 20 Ball:Destroy() end end)
((This is literally my third question about the same script lol)) I'm trying to damage a player when this attack happens, but it do any damage and can't find the human, How do I fix it?
There are many issues in this.
--DEFINE ball. Example: local Ball = script.Parent --CHANGE THIS TO FIND WHERE BALL IS. Ball.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") -- you do not need 2 parent searches, you'll be looking into workspace. if a part of the character touches the ball, it will be a child of the character, the same as humanoid. if hum ~= nil then -- Nil means "nothing". So what you were saying is "if hum is nothing, then:" .. But what you want, is " if hum exists, then:" so you need the opposite. ~= means unequal. So "if hum ~= nil then" translates to: "if hum is NOT 'nothing' (so it exists), then:) -- " hit.Health = hit.Health - 20 " does not work. hit is just a child of the character. Try this: hum.Health = human.Health - 20 Ball:Destroy() end end)
Also, consider adding a debounce. I can explain how that works if you wish. I typed this out here, so there are bound to be mistakes such as typos. Have a good day.
iamnoamesa's script has very good explanations (so be sure to read it!). However, there is a flaw: in games where projectiles might remove themselves when they hit something, you must always make sure that "hit.Parent" is not nil; otherwise, if your ball happens to hit such a projectile, the script will try to index a nil value.
One extra point of note: if hum ~= nil then
is practically the same as if hum then
(unless hum
might be false
). You can put anything inside an if then
and it'll be considered true
so long as the value is neither nil
nor false
. There is nothing wrong with if hum ~= nil then
, however, it's just longer.
So, a possible fixed script (assuming you define Ball
somewhere, like iamnoamesa says) would be:
Ball.Touched:connect(function(hit) if not hit.Parent then return end local hum = hit.Parent:FindFirstChild("Humanoid") if not hum then return end hum:TakeDamage(20) Ball:Destroy() end)
if not hit.Parent then return end
is the only line missing from iamnoamesa's script. Other changes are unimportant.
Health is not part of hit, but part of hum(humanoid)
Try ~~~~~~~~~~~~~~~~~ if Humanoid ~= nil then
Or
if humanoid ~= nil then ~~~~~~~~~~~~~~~~~ Both of these apply to line 4.