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

How to use a sword to kill NPC only?

Asked by 6 years ago

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.

0
That didn't help the problem, it just simply takes damage the minute I pull out the sword. robloxquestionnaire1 30 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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)
0
Where do i put this Script? moomaxy31st 0 — 3y
Ad

Answer this question