Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I fix this damage script when a attack hits a player?

Asked by
Kitorari 266 Moderation Voter
7 years ago
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?

0
If you wish to respect forcefields, use "hum:TakeDamage(20)" instead of "hum.Health = hum.Health - 20" chess123mate 5873 — 7y

4 answers

Log in to vote
3
Answered by 7 years ago

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.

1
Some notes: "if hum ~= nil then" is the same as "if hum then" (unless "hum" is false). Debounce is unnecessary since there are no wait commands (and the Ball is destroyed if it hits a target). You need to make sure hit.Parent isn't nil (in case it removed itself on Hit, much like this Ball does). Nonetheless, good comments, so I gave a +1. chess123mate 5873 — 7y
Ad
Log in to vote
3
Answered by 7 years ago
Edited 7 years ago

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.

0
My script wasn't really good, as that's not what I was attempting to do. I was trying to explain why his doesn't work so he understands. That's why I used simpler terms. (like saying "if hum ~= nil then" instead) "if hum then"... Anyways, thanks and have a good day :) +1 from me too iamnoamesa 674 — 7y
Log in to vote
1
Answered by
mnaj22 44
7 years ago

Health is not part of hit, but part of hum(humanoid)

0
Also, I'm pretty sure nil means nothing, so your if statement says "if hum == nothing then" so it can find the humanoid. (I'm not 100% sure about this part) mnaj22 44 — 7y
0
can't* mnaj22 44 — 7y
0
Now its trying to index a nill value...' Kitorari 266 — 7y
0
Most damage scripts have that, I'm still not sure how necessary it is. Kitorari 266 — 7y
0
The checker is important to see if it is a player or not. SH_Helper 61 — 7y
Log in to vote
1
Answered by 7 years ago

Try ~~~~~~~~~~~~~~~~~ if Humanoid ~= nil then

Or

if humanoid ~= nil then ~~~~~~~~~~~~~~~~~ Both of these apply to line 4.

Answer this question