-
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. -
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! -
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)
- When the math.random(-25, 25), in the Vector3, is math.random(1, 25) the enemy moved.
- 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!