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 NPCs not hurt eachother with swords?

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

✓ Accepted answer

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. 

2 comments

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.

0

Makes sense.

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.

3 comments

0

They're still hurting each other it seems.

0

Fixed it

0

Oh wait, my mistake, I added something by mistake. Thanks man!

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