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

How to make a NPC keep its distance from Zombies?

I need to make a NPC have its walkspeed set to 0 when it is close 20 studs or less to another NPC with its humanoid labeled "Zombie", so far I havn't managed to do it, if the NPC can walk away then it will be even better.

1 answer

Do you want the NPC to walk away from the Zombie or have its WalkSpeed set to 0? Because the NPC can't really move if it has 0 WalkSpeed, if you want to set the Walkspeed to 0 then its really easy.

wait(1)

print('Script Loaded')


local NormalSpeed = script.Parent.Humanoid.WalkSpeed
function FindChild(pos)
	local ListOfObj = workspace:GetChildren()
	local Dist = math.random(20, 40)
	local Found = nil
	local IsHuman = nil
	local HumanPart = nil
	local Torso = nil
	for x = 1, #ListOfObj do
		Found = ListOfObj[x]
		if (Found.ClassName == "Model") and (Found ~= script.Parent) then
			IsHuman = Found:FindFirstChild("Zombie")
			HumanPart = Found:FindFirstChild("Torso")
			if (HumanPart ~= nil) and (IsHuman ~= nil) and (IsHuman.Health ~= 0) then
				if (HumanPart.Position - pos).magnitude < Dist then
					Torso = HumanPart
					Dist = (HumanPart.Position - pos).magnitude
				end
			end
		end
	end
	return Torso
end

while true do
	wait()
	local Dir_Pos = FindChild(script.Parent.Torso.Position)
	if Dir_Pos ~= nil then
		print 'Zombie was found!'
		script.Parent.Humanoid.WalkSpeed = 0
	else
		script.Parent.Humanoid.WalkSpeed = NormalSpeed
	end
end

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