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

How to use a sword to kill NPC only?

Using a script online. script.Parent.blade.Touched:connect(function(p) if script.Parent.CanDamage.Value == true then p.Parent.Humanoid:TakeDamage(20) script.Parent.CanDamage.Value = false end end)

The NPC humanoid part is named 'NPC'

IN THE script it says "Humanoid is not recognized"

but when I use

p.Parent.Henchmen.NPC:TakeDamage(20)

(the model is named henchmen and the humanoid part is named 'NPC')

When I swing the sword, it actually takes damage from the NPC far away without even touching it.

Thanks for help in advance.

1 comment

0

That didn't help the problem, it just simply takes damage the minute I pull out the sword.

1 answer

✓ Accepted answer

You need to use something called GetPlayerFromCharacter. If we get a player, we ignore it, else we kill it. Don't rename your humanoid, somethings don't work when you rename the Humanoid.

script.Parent.Blade.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") then
        if game:GetService("Players"):GetPlayerFromCharacter(part.Parent) then -- make sure we can get the player from it
            local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
            if plr then 
                -- do nothing, it's a Player's character
            elseif not plr then -- just to make sure it's not a player
                part.Parent.Humanoid:TakeDamage(20)
            end
        end
    end
end)

1 comment

0

Where do i put this Script?

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