Zombie System Isnt Functioning Properly?

What I Am Creating Currently.

Hi So Currently Im Working On Creating A Wandering System With My Zombie NPC So That When A Player Gets Close To It The NPC Follows The Player Until The Player Dies.

What Im Having Troubles With.

So What Im Having Issues With Is The NPC Wanders But It Doesnt Detect If A Player Gets Close To It In The Range Of Distance Variable Located In The Code Below.

local IsWandering = true
local ZombiePrimary = script.Parent.HumanoidRootPart
local Zombie = script.Parent
local Humanoid = script.Parent.Humanoid
local PathfindingService = game:GetService("PathfindingService")
local Goal = game.ReplicatedStorage["Wandering Location"]

function KillPlayer()
	for _, ZombieParts in pairs(Zombie:GetChildren()) do
		if ZombieParts:IsA("Part") or ZombieParts:IsA("BasePart") then
			ZombieParts.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					local Humanoid = hit.Parent:FindFirstChild("Humanoid")
					Humanoid:TakeDamage(100)
				end
			end)
		end
	end
end

function Wander()
	-- Generate a random position within a larger radius around the zombie
	local randomRadius = 65.5 -- Increase the radius to make it move farther
	local randomPosition = ZombiePrimary.Position + Vector3.new(
		math.random(-randomRadius, randomRadius),
		0,
		math.random(-randomRadius, randomRadius)
	)

	-- Find a path to the random position
	local path = PathfindingService:CreatePath()
	path:ComputeAsync(ZombiePrimary.Position, randomPosition)
	local waypoints = path:GetWaypoints()

	-- Move the zombie along the path
	for _, waypoint in ipairs(waypoints) do
		Humanoid:MoveTo(waypoint.Position)
		Humanoid.MoveToFinished:Wait() -- Wait until the zombie reaches the waypoint
	end
end

while IsWandering do
	if IsWandering == true then
		Wander()
		for i, plr in ipairs(game.Players:GetPlayers()) do
			local Character = plr.Character 
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			local DistanceBetween = (HumanoidRootPart.Position - ZombiePrimary.Position).magnitude
			local Distance = 100

			if DistanceBetween < Distance then
				Humanoid:MoveTo(HumanoidRootPart.Position)
				if Humanoid.Health == 0 then
					IsWandering = false
				end
			end
		end
	end
end

2 Likes

the wander function is yielding the whole loop with this line:

place the wander function in a new thread and add a check if the npc is already moving somewhere or just make a new while loop for it

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.