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 the NPCs not attack eachother?

Im using this script

wait(1.5)
local sp = script.Parent
local Humanoid = sp:WaitForChild("Humanoid")
local Head = sp:WaitForChild("Head")
local Torso = sp:WaitForChild("Torso")
local Animation = script:WaitForChild("AttackAnim")
local Zombie = Instance.new("Sound",Torso)
Zombie.Volume = 1.5
Zombie.SoundId = "http://www.roblox.com/asset/?id=266272124"
spawn(function() Zombie:Play() end)
local Hit1 = Instance.new("Sound",Torso)
Hit1.Volume = 2
Hit1.SoundId = "http://www.roblox.com/asset/?id=4059009185"
local Hit2 = Instance.new("Sound",Torso)
Hit2.Volume = 2
Hit2.SoundId = "http://www.roblox.com/asset/?id=5775721420"

local AttackEnabled = true

function waiting(TimeToWait)
	if TimeToWait ~= nil then
		local TotalTime = 0
		TotalTime = TotalTime + game:GetService("RunService").Heartbeat:waiting()
		while TotalTime < TimeToWait do
			TotalTime = TotalTime + game:GetService("RunService").Heartbeat:waiting()
		end
	else
		game:GetService("RunService").Heartbeat:waiting()
	end
end

function DamageTag(parent,damage)
	local DmgTag = script.DamageTag:clone()
	DmgTag.Damage.Value = damage
	DmgTag.creator.Value = game.Players.LocalPlayer
	DmgTag.Disabled = false
	DmgTag.Parent = parent
end

local Anim = Humanoid:LoadAnimation(Animation)
function Hit(hit)
	wait(0.5) local TargetHum = hit.Parent:FindFirstChild("Humanoid")
	if TargetHum ~= nil and TargetHum:IsA("Humanoid") and AttackEnabled == true and TargetHum ~= Humanoid then
		AttackEnabled = false
		delay(0.5,function() AttackEnabled = true end)
		if Anim then Anim:Play(nil,nil,2) end
		DamageTag(TargetHum.Parent,30)
		wait(0.1)
		Hit1:Play()
		wait(0.1)
		Hit2:Play()
	end
end

for _, Child in pairs(script.Parent:GetChildren()) do
	if Child:IsA("Part") or Child:IsA("WedgePart") or Child:IsA("CornerWedgePart") then
		Child.Touched:connect(Hit)
	end
end 

This is a script to make the zombies attack any humanoid they come in contact with. The script i'm looking for is one to attack only players without having to change the name of the humanoid

2 comments

0

PLEASE but please put a code block

0

Sorry, this is my first time asking a question

1 answer

✓ Accepted answer

If you only want them to attack players, see my answer here for how to target only players: https://scriptinghelpers.org/questions/118377

For the script you've pasted, you presumably want to modify the Hit function so that it doesn't do anything if hit belongs to a zombie. An easy way to check is to see if hit.Parent exists and if its name is Zombie, like so:

function Hit(hit)
	if not hit.Parent or hit.Parent.Name == "Zombie" then return end
	-- rest of Hit function here
end

6 comments

0

This kinda works, they aren't hitting each other but when one of them hit me, they also hit the other zombie at the same time

0

Are all zombies named exactly "Zombie"? If you're not sure, you could put the following before the 'if' statement and then recreate the scenario : print(hit:GetFullName())

0

If that doesn't lead anywhere (because it only ever prints out parts belonging to your character and not other zombies), you have to find if there are any other scripts that cause damage. For instance, if you have 2+ zombies, do they all have the updated code? (If not, consider using ModuleScripts or cloning a Script into each Zombie so you can just make the changes in one place)

0

I tested this using the exact same zombie, they don't go after each other but they can still hit each other. I believe the problem causing this is at the bottom of the script but I am unsure what to do about it. This script has a child named damage tag but I don't know if that has to do with it or not.

0

Wow, I made a silly mistake - it should be or hit.Parent.Name == "Zombie", not or hit.Parent == "Zombie"! (I'll update the answer)

0

This works! Thank you.

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