Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make my npc only do damage to players and not other npcs?

So all my npc's run at me but they do damage to eachother aswell killing them before they kill me. How do I fix this. Kill/damage script below.

local debounce = false
local function onTouch(Touch)
    if Touch.Parent:FindFirstChild("Humanoid") ~= nil then
    if not debounce then
        debounce = false
            Touch.Parent.Humanoid.Health =  Touch.Parent.Humanoid.Health - .15
        wait(1)
        debounce = false
    end
    end
end

script.Parent.HumanoidRootPart.Touched:Connect(onTouch)

2 answers

✓ Accepted answer

Just use game.Players:GetPlayerFromCharacter()

local debounce = false
local function onTouch(Touch)
    if game.Players:GetPlayerFromCharacter(Touch.Parent) then
    print("Touched player, giving damage.")
    if Touch.Parent:FindFirstChild("Humanoid") ~= nil then
    if not debounce then
        debounce = false
            Touch.Parent.Humanoid.Health =  Touch.Parent.Humanoid.Health - .15
        wait(1)
        debounce = false
    end
    end
    else print("Was trying to damage an NPC, it was blocked.")
    end
end

script.Parent.HumanoidRootPart.Touched:Connect(onTouch)

I also enhanced it a little too.

2 comments

0

It worked! Thanks! I had to removed the print though because the spam was real

0

oh thats ok

This discussion is preserved as it was. New answers, comments, and votes are closed.