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

How do I make NPCs not hurt eachother with swords?

Asked by 10 years ago

Hey there. So, in this game I'm currently developing, I have these enemy NPCs that you must attack. Right now, I've just added swords to the enemies, but they seem to cause damage to each other when the sword goes near them. This is the one part of the code from the LinkedSwords I've edited from them.

--Beginning sword code here

function blow(hit)
    if (hit.Parent == nil) and (hit.Parent == "Blooborn") then return end -- happens when bullet hits sword

    local humanoid = hit.Parent:findFirstChild("Humanoid")
    local vCharacter = Tool.Parent
    local vPlayer = game.Players:playerFromCharacter(vCharacter)
    local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
    if humanoid~=nil and humanoid ~= hum and hum ~= nil then
        -- final check, make sure sword is in-hand

        local right_arm = vCharacter:FindFirstChild("Right Arm")
        if (right_arm ~= nil) then
            local joint = right_arm:FindFirstChild("RightGrip")
            if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
                tagHumanoid(humanoid, vPlayer)
                humanoid:TakeDamage(damage)
                wait(1)
                untagHumanoid(humanoid)
            end
        end


    end
end

--The rest of the sword code here

I'm not sure if I edited it correctly, so, my question is: How would I fix this part of the code? Cause I don't think Flow Control would have a key part in this..

2 answers

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

That's not the best way to do it, because then 'NPC' will be above every enemies' head, which is probably not what he wants.

To do what I think you want, you need to check if it is a player. This can be easily done by using game.Players:GetPlayerFromCharacter(character)

Therefore, you can just simply do this:

if not game.Players:GetPlayerFromCharacter(humanoid.Parent) then return end --Stops the function if the player does not exist. 
0
In his defense, all you'd really have to do is just change "NPC" to the name of your NPC's model. But, I do like how your method is way more simpler. AcidGamer64 10 — 10y
0
Makes sense. HexC3D 830 — 10y
Ad
Log in to vote
1
Answered by
HexC3D 830 Moderation Voter
10 years ago

Change the NPC's name to NPC(Model Name to NPC)

--Beginning sword code here

function blow(hit)
    if (hit.Parent == nil) and (hit.Parent == "Blooborn") then return end -- happens when bullet hits sword

    local humanoid = hit.Parent:findFirstChild("Humanoid")
    local vCharacter = Tool.Parent
    local vPlayer = game.Players:playerFromCharacter(vCharacter)
    local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
    if humanoid~=nil and humanoid ~= hum and hum ~= nil then
        -- final check, make sure sword is in-hand

        local right_arm = vCharacter:FindFirstChild("Right Arm")
        if (right_arm ~= nil) then
            local joint = right_arm:FindFirstChild("RightGrip")
            if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
                tagHumanoid(humanoid, vPlayer)
if humanoid.Parent.Name == "NPC" then
print("Safe")
else
                humanoid:TakeDamage(damage)
                wait(1)
                untagHumanoid(humanoid)
            end
        end
end

    end
end

--The rest of the sword code here

If this helped +1, and make sure the website knows I answered k.

0
They're still hurting each other it seems. AcidGamer64 10 — 10y
0
Fixed it HexC3D 830 — 10y
0
Oh wait, my mistake, I added something by mistake. Thanks man! AcidGamer64 10 — 10y

Answer this question