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

Doesn't work in a NPC but works inside a part, why?

Asked by 6 years ago

So, I'm creating a zombie NPC that damages a player on touch and the script worked perfectly fine until I decided to add a debounce to it. The script still works, however, the script works inside any part that isn't inside a NPC. Once it's placed inside a part inside a NPC it won't work. I don't understand, someone please help!

debounce = false
script.Parent.Touched:connect(function(hit)
    h = hit.Parent:FindFirstChild("Humanoid")
    if debounce == false then 
        debounce = true 
        h.Health = h.Health - 50
        wait(2)
        debounce = false
    end
end)
0
Make sure to check that the 'hit' has a Humanoid, because if it touches an accessory, the script world break, since it wouldn't be able to find a humanoid in the accessory. Crazycat4360 115 — 6y
0
The torso or whatever this script is inside of is most likely hitting the other parts of the NPC, causing the debounce to go off. getzedotus 0 — 6y
0
Do you know how I could fix that? Michael_TheCreator 166 — 6y

1 answer

Log in to vote
0
Answered by
CPF2 406 Moderation Voter
6 years ago
debounce = false

for i, v in pairs(script.Parent:GetChildren()) do
    if v.ClassName == "Part" then
        v.Touched:connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                local h = hit.Parent:FindFirstChild("Humanoid")
                if debounce == false then 
                    debounce = true 
                    h.Health = h.Health - 50
                    wait(2)
                    debounce = false
                end
            end

        end)
    end
end

Put that script inside of the NPC

0
It's still not working! D: Michael_TheCreator 166 — 6y
0
Are you putting the script inside of the NPC's model? (Not the humanoid) CPF2 406 — 6y
0
Thank you for clarifying. I was placing the script inside of a part located in the NPC. Michael_TheCreator 166 — 6y
Ad

Answer this question