I’m making a tower defense game, and the problem is after I modified my enemy moving script, they didn’t die and damage the base when they reached the end.
What’s wrong with it?
Here’s my move script:
local ServerStorage = game:GetService(“ServerStorage”)
local PhysicsService = game:GetService(“PhysicsService”)
local TweenService = game:GetService(“TweenService”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local mob = {}
function mob.Move(mob, map, offset, MovingToValue)
if MovingToValue == nil then
local humanoid = mob.Humanoid
local waypoints = map.Waypoints
for waypoint=mob.MovingTo.Value, #waypoints:GetChildren() do
mob.MovingTo.Value = waypoint
repeat humanoid:MoveTo(waypoints[waypoint].Position + offset)
until humanoid.MoveToFinished:Wait()
end
mob:Destroy()
map.Base.Humanoid:TakeDamage(humanoid.Health)
else
local humanoid = mob.Humanoid
local waypoints = map.Waypoints
for waypoint = MovingToValue, #waypoints:GetChildren() do
mob.MovingTo.Value = waypoint
repeat humanoid:MoveTo(waypoints[waypoint].Position + offset)
until humanoid.MoveToFinished:Wait()
end
mob:Destroy()
map.Base.Humanoid:TakeDamage(humanoid.Health)
end
Like @niceshark11 said, you have an error which looks for the 11th way point, this is probably because in your for loop you define waypoint to equal mob.MovingTo.Value, waypoint corresponds with #waypoints:GetChildren(), so this must mean that your waypoint variable is larger than the amount of waypoints.
To avoid this error, you can add an extra condition at both your until statements:
repeat humanoid:MoveTo(waypoints[waypoint].Position + offset)
until (humanoid.MoveToFinished:Wait() or mob.MovingTo.Value == 10)
You are writing 2 loops (that do the same thing) inside of each other, you don’t need a repeat until if your using a for loop and vise versa, it looks to be that it’s best to choose one over the other cause your just looping over the same thing twice.
try this:
local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mob = {}
function mob.Move(mob, map, offset, MovingToValue)
if MovingToValue == nil then
local humanoid = mob.Humanoid
local waypoints = map.Waypoints
local waypoint = mob.MovingTo.Value
repeat humanoid:MoveTo(waypoints[waypoint].Position + offset)
until (humanoid.MoveToFinished:Wait() or waypoint == 10)
mob:Destroy()
map.Base.Humanoid:TakeDamage(humanoid.Health)
else
local humanoid = mob.Humanoid
local waypoints = map.Waypoints
local waypoint = mob.MovingTo.Value
repeat humanoid:MoveTo(waypoints[waypoint].Position + offset)
until (humanoid.MoveToFinished:Wait() or waypoint == 10)
mob:Destroy()
map.Base.Humanoid:TakeDamage(humanoid.Health)
end
end
(Thanks for the help btw) I finally fixed it after realizing that any scripts after the repeated MoveTo would not work because the script would error once the repeat reached waypoint 11, making any script after that useless. (so I made it check before the MoveTo)
Solution :
not the greatest, but works.
function mob.Move(mob, map, offset, MovingToValue)
if MovingToValue == nil then
local humanoid = mob.Humanoid
local waypoints = map.Waypoints
for waypoint=mob.MovingTo.Value, #waypoints:GetChildren() do
mob.MovingTo.Value = waypoint
repeat
if mob.MovingTo.Value == 11 then
mob:Destroy()
map.Base.Humanoid:TakeDamage(humanoid.Health)
end
humanoid:MoveTo(waypoints[waypoint].Position + offset)
until humanoid.MoveToFinished:Wait()
end
else
local humanoid = mob.Humanoid
local waypoints = map.Waypoints
for waypoint = MovingToValue, #waypoints:GetChildren() do
mob.MovingTo.Value = waypoint
repeat
humanoid:MoveTo(waypoints[waypoint].Position + offset)
if mob.MovingTo.Value == 11 then
mob:Destroy()
map.Base.Humanoid:TakeDamage(humanoid.Health)
end
until humanoid.MoveToFinished:Wait()
end
mob:Destroy()
map.Base.Humanoid:TakeDamage(humanoid.Health)
end
end