This is just a damage script I put in my AI. I'm not sure why but when I hit them with a sword, there is no error but error appeared when I shot them with a bow. I guess that they computer might thought that I mean it differently because I clone an arrow into my tool when I shoot.
local cool = false local slash = script:WaitForChild("Slash") local human = script.Parent.Parent:FindFirstChildWhichIsA("Humanoid") local anim2 = human:LoadAnimation(slash) script.Parent.Touched:Connect(function(hit) if cool == false and script ~= nil then if hit.Parent:FindFirstChildOfClass("Humanoid") then -- error here local enemy = hit.Parent:FindFirstChildOfClass("Humanoid") if hit.Parent ~= script.Parent.Parent and enemy.Name ~= human.Name then cool = true anim2:Play() enemy.Health = enemy.Health - 15 wait(2) cool = false end end end end)
Help please. Thankyou.
I might've found the answer:
I theorize that you parented the arrow projectile to the player's character, programmed to remove after touching something, and then the AI throws an error because it did not catch up with the disappearance of the projectile, thinking it is nil because it fired after the arrow is gone?
Maybe try a different approach to this as I don't recommend using this method since it comes with cons on multiplayer games, most notably when the AI touches the player or vice versa because it'll look for an object that belongs to players, and theplayer's body parts are considered.
I have not made a game with weapons so I might be not much of help on this genre, but if you just want the AI to be hurt when an arrow gets hit, and that you're using the default tool system, consider using the projectile instead for damage, like when the projectile hits something, check if it is an AI or something else. If it is an AI, use the humanoid reference and deduct with Humanoid:TakeDamage(<amountofdamage>), otherwise ignore. After the touch, remove the projectile.
Hope this helps even if I may have not explained well.
"hit" is usually the part that hit it. If the hitpart, otherwise, the part that hit, has no instance under its parent that's called "Humanoid", the value will return nil and thus it will error. >hit.Parent:FindFirstChildOfClass("Humanoid") only works for players that touched the script's Parent.