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

Why is my damage script not working even though it works in other parts of the game?

Asked by 4 years ago

I need this script to damage the NPC but it's not working even though it works in every other part of the game and there are no errors but they are not taking damage pls help

function onTouched(hit)
        local holy = hit.Parent:FindFirstChild("holy")
        if (holy ~= nil) then
                holy.Health = holy.Health - 17500
        end
end
script.Parent.Touched:Connect(onTouched)
0
Mind if you put and else statement and print something in the else part and see if it prints. jonathanpecany100 0 — 4y
0
you dont need to put the ~= nil because FindFirstChild is already checking if hit.Parent exists laurenbtd5 379 — 4y
0
hmm ok but this script works for damaging players but when i try it for npc it doesnt work coolmanHDMI 72 — 4y
0
is it a LocalScript? if it is, it should be a normal script if it's in workspace. Mayk728 855 — 4y
View all comments (6 more)
0
its a normal script and it always works but i guess since the name of the npcs humanoid is different maybe it doesent work? coolmanHDMI 72 — 4y
0
I am wondering as to where your script is at? Maybe if we know, it could be a bit better as you only put script.parent jonathanpecany100 0 — 4y
0
i mean, yeah, if the target NPC's humanoid name isn't "holy" as described in the script, of course it isn't gonna work. Mayk728 855 — 4y
0
I do agree with Mayk78. Try printing out the holy variable and see if the target is the target you want it to target. jonathanpecany100 0 — 4y
0
no the npcs name is holy im saying maybe because its not "humanoid" maybe it doesnt work coolmanHDMI 72 — 4y
0
So the name of the humanoid is "holy", don't see the point in naming the humanoid. It is quite hard to help as your question isn't that much descriptive. jonathanpecany100 0 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Well...depending on the NPC, the NPC might have extra parts subbed under parts and trying to find the holy part.

This question isn't actually much in detail so trying my best and if you add more detail, I'll add more detail into this answer. But I can give some debugging tips of what to do if you get no output.

The first debugging tip is to print variables and see if they are nil or not, especially if you use an if statement to check.

Another debugging method also helps when ever you are trying to get a part's children and that is to use the for loop to see all the children and see if that part is even in it.

It is good to put as much print with variables in it for debugging, but remember to remove them when you found the error as depending on what you print, it could be a security issue in your game as it logs the game on the player as well. As I have use before.

Here is a link to how you can make a good question on here. https://scriptinghelpers.org/help/how-post-good-questions-answers

Ad
Log in to vote
0
Answered by
Mayk728 855 Moderation Voter
4 years ago
Edited 4 years ago

Here's something you can try:

script.Parent.Touched:Connect(function(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChild('holy') then
        local h = hit.Parent.holy
        h:TakeDamage(17500)
    end
end)

If there are NPC's with different humanoid names, you could use a loop to find an object with the classname "Humanoid", so the name of it won't matter.

script.Parent.Touched:Connect(function(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChildOfClass('Humanoid') then
        local Humanoid = nil
        for i,v in pairs(hit.Parent:GetChildren()) do
            if v.ClassName == 'Humanoid' then
                Humanoid = v
            end
        end
        if Humanoid ~= nil then
            Humanoid:TakeDamage(17500)
        end
    end
end)

Answer this question