Urgent - My enemy is not moving!

  1. What do you want to achieve?
    I want my enemy to saunter around when its day and chase the player when its night. The script I wrote worked perfectly except for one thing.

  2. What is the issue?
    The enemies all walked towards a common point and clustered up. I simply cannot have this in my game. I noticed this after I sponsored the game!

  3. What solutions have you tried so far?
    The code worked when a value was positive but not negative. But, again, they all clustered up. So, really, it did not work either. I have provided the code below. (It is place directly in the enemy model. Sorry if its a big long.)

local zombie = script.Parent
local maxDistance = 100
local target
local night = game.ReplicatedStorage.Night.DayorNight

zombie:WaitForChild("Humanoid").DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None

wait(3)
function findPlayer()
	for i, player in pairs(game.Players:GetChildren()) do
		if (player.Character.Torso.Position - zombie.Torso.Position).Magnitude <= 100 then

			target = player.Character.Torso
			return target
		end		
	end
	return false
end

spawn(function()
	while wait() do
		local playerTarget = findPlayer()
		if playerTarget ~= false then
			if night.Value == "Night" then
				zombie.Humanoid.WalkSpeed = 35
				zombie.Humanoid:MoveTo(playerTarget.Position, playerTarget)
			else
				print("It is morning!")
				zombie.Humanoid:MoveTo(Vector3.new(math.random(-25, 25), 0, math.random(-25, 25)))
				zombie.Humanoid.WalkSpeed = 10
				wait()
			end	
		else
			zombie.Humanoid:MoveTo(Vector3.new(math.random(-25, 25), 0, math.random(-25, 25)))
			zombie.Humanoid.WalkSpeed = 10
			wait()
		end
	end
end)

local debounce = false

zombie["Right Arm"].Touched:Connect(function(hit)
	print("Right arm touched!")
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Name ~= zombie.Name then
			print("We can kill!")
			if not debounce and night.Value == "Night" then
				debounce = true
				
				hit.Parent.Humanoid:TakeDamage(25)
				
				wait(2)
				debounce = false				
			end
		end
	end
end)

zombie["Left Arm"].Touched:Connect(function(hit)
	print("Left arm touched!")
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Name ~= zombie.Name then
			print("We can kill!")
			if not debounce and night.Value == "Night" then
				debounce = true

				hit.Parent.Humanoid:TakeDamage(25)

				wait(2)
				debounce = false				
			end
		end
	end
end)

  1. When the math.random(-25, 25), in the Vector3, is math.random(1, 25) the enemy moved.
  2. When the math.random(-25, 25) in the Vector3, is math.random(-25, 25) the enemy does not move.

What should I do? Your replies are much appreciated!

The Vector3.new constructor only will always make the Zombie move relative to its world position so it’s always gonna move to the center of the place

Could you try this?

local zombie = script.Parent
local maxDistance = 100
local target
local night = game.ReplicatedStorage.Night.DayorNight

zombie:WaitForChild("Humanoid").DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None

wait(3)
function findPlayer()
	for i, player in pairs(game.Players:GetChildren()) do
		if (player.Character.Torso.Position - zombie.Torso.Position).Magnitude <= 100 then

			target = player.Character.Torso
			return target
		end		
	end
	return false
end

spawn(function()
	while wait() do
		local playerTarget = findPlayer()
		if playerTarget ~= false then
			if night.Value == "Night" then
				zombie.Humanoid.WalkSpeed = 35
				zombie.Humanoid:MoveTo(playerTarget.Position, playerTarget)
			else
				print("It is morning!")
				zombie.Humanoid:MoveTo(zombie.Torso.Position + Vector3.new(math.random(-25, 25), 0, math.random(-25, 25)))
				zombie.Humanoid.WalkSpeed = 10
				wait()
			end	
		else
			zombie.Humanoid:MoveTo(zombie.Torso.Position + Vector3.new(math.random(-25, 25), 0, math.random(-25, 25)))
			zombie.Humanoid.WalkSpeed = 10
			wait()
		end
	end
end)
1 Like

I just realized I should have added the position of the enemy right before I read your post. I’m an idiot. Sorry for using your time. Thanks for the reply!

Aha you’re fine we all make mistakes from time to time :sweat_smile: But yeah just make sure to keep that in mind

1 Like