NPC not hurting player when I put blocking if statement

Hey, I’ve had this issue where the NPC wouldn’t hurt the player when I put an if statement that detects if a value in the player’s tool was true however, this completly breaks the npc and it doesn’t hurt me.
The NPC script is in workspace, and I am setting the blocking value on the server.

Here is the script part:

HitBox.OnHit:Connect(function(hit,humanoid)
            if hit.Parent:FindFirstChild("AI") then hit.Parent:FindFirstChild("AI").Target.Value = NPC.Name end
            if hit.Parent.Name == "Sword" or hit.Parent:FindFirstChild("Values").Blocking.Value == true then  
                if humanoid.Health == 0 then return end

                ClashSound[math.random(1,#ClashSound)]:Play() -- Play clashing sound

                humanoid:TakeDamage(math.random(10,25)) -- Take less damage

                local BodyPush = Instance.new("BodyVelocity",humanoid.Parent.HumanoidRootPart)
                BodyPush.Velocity = Vector3.new(-25,-3,-25)
                BodyPush.MaxForce = Vector3.new(100000,100000,100000)

                local Blur = Instance.new("BlurEffect",game.Lighting)
                Blur.Size = 15

                local Sparks = ClashParticle:Clone()
                Sparks.Parent = hit:FindFirstChild("Clash")

                local ClashLight = ClashLight:Clone()
                ClashLight.Parent = hit:FindFirstChild("Clash")

                CameraEvent:FireAllClients("Clash")

                Debris:AddItem(Blur,0.14)
                Debris:AddItem(BodyPush,0.07)
                Debris:AddItem(ClashLight,0.1)
                Debris:AddItem(Sparks,0.5)
            else
                humanoid:TakeDamage(math.random(Damage.Value,MaxDamage.Value)) -- Player hit a person so he will get hurt

                local ImpactSound = ImpactSounds[math.random(1,#ImpactSounds)]:Clone()
                ImpactSound.Parent = hit
                ImpactSound:Play()

                Debris:AddItem(ImpactSound,ImpactSound.TimeLength)
                BloodClone.Parent = hit
            end
        end)

If anyone can help that would be wonderful, this issue has been really annoying!

2 Likes

Does it play the impact sound though? If it does but it doesn’t damage you then the value that interacts with the damage is the problem.

1 Like

Nope it doesnt, It just skips the OnHit event for some reason when I check for that value.

Just found the bug, “else” basically mirrors the previous nearest if statement behind it so if you change it to “elseif hit.Parent.Name == “Sword” or hit.Parent:FindFirstChild(“Values”).Blocking.Value == false then”, then it’ll work.

The script thinks you’re trying to find a non named “Sword” part while having the value false.

Ah, but there is no other if statement before it? Thats the first one. So shall I put the second on to the first?

Only edit this part with the changed codeline i gave from my previous post. (im talking about the else in the middle)

I don’t get what you mean? You want me to write:

elseif hit.Parent.Name == “Sword” or hit.Parent:FindFirstChild(“Values”).Blocking.Value == false then

? That line is to detect if the player is blocking but the else is when he is not blocking.

if hit.Parent.Name == "Sword" or hit.Parent:FindFirstChild("Values").Blocking.Value == true then

You’re using a or statement which means the script is checking if the parent’s name is sword, or if blocking is true. However the main problem with your code is this part right here:

 hit.Parent:FindFirstChild("Values").Blocking.Value == true

Your script is making the assumption that whatever it’s hitting has a thing called Values in it and has a tag called Blocking in it, which may not be the case which may be the cause of your code breaking. If your code breaks there, it won’t continue ahead.

So for example, if you attacked an enemy on the leg, his character might not have something called ‘Values’ inside that character, which will break your script since you’re using an or statement.

1 Like

Back, that’s not the problem since it would get the parent of the part that triggered the hit function which would then not be the case.

@babyboomer14 I’ll explain it to you more derogatory, in the 2nd if statement, you’re telling the script to check if the part the hitbox hits is named “Sword” or if it’s blocking value is turned on, if it meets the requirement it would basically let the other code go, in the else statement, it thinks you’re trying to make it check if the part it hits isn’t named “Sword” or if it doesn’t have the blocking value, turned on.

Now this is where it gets confusing, the script would let the next code run if it meets the requirement vice versa, since you used “or” there may be unmentioned flaws such as parts having unintended impostor values which causes this bug.

Or if i’m wrong then that means i’m probably too tired and sleepy and @somedeviser is probably correct.

1 Like