Hello, I’m trying to make a tower defense game.
I’m trying to use TweenService for the movement system but i cannot figure out how to get the previous waypoint.
function EnemyModule.Move(enemy)
for waypoint = 1, #workspace.Waypoints:GetChildren() do
local previousPoint = workspace.Waypoints[0]
local nextPoint = workspace.Waypoints[1]
local Distance = (previousPoint.Position-nextPoint.Position).Magnitude
local TravelTime = Distance / enemy.Humanoid.WalkSpeed
local MoveAnimation = TS:Create(enemy:WaitForChild("HumanoidRootPart"), TweenInfo.new(TravelTime), {["CFrame"] = workspace.Waypoints[waypoint].CFrame})
MoveAnimation:Play()
MoveAnimation.Completed:Wait()
end
end
This is the error I’m getting.
If you know the solution to this please let me know.
local waypoints = #workspace.Waypoints:GetChildren()
function EnemyModule.Move(enemy)
for waypoint = 2, waypoints do
local previousPoint = workspace.Waypoints[waypoint - 1]
local nextPoint = workspace.Waypoints[waypoint]
local Distance = (previousPoint.Position-nextPoint.Position).Magnitude
local TravelTime = Distance / enemy.Humanoid.WalkSpeed
local MoveAnimation = TS:Create(enemy:WaitForChild("HumanoidRootPart"), TweenInfo.new(TravelTime), {["CFrame"] = workspace.Waypoints[waypoint].CFrame})
MoveAnimation:Play()
MoveAnimation.Completed:Wait()
end
end
waypoint 1 would be the starting point.
or …
for waypoint = 1, waypoints do
local previousPoint = workspace.Waypoints[waypoint]
local NextPoint = workspace.Waypoints[waypoint + 1]
function EnemyModule.Move(enemy)
for waypoint = 1, #workspace.Waypoints:GetChildren() do
local currentWaypoint = workspace.Waypoints[tostring(waypoint)]
local nextPoint = workspace.Waypoints[tostring(waypoint + 1)]
local Distance = (currentPoint.Position-nextPoint.Position).Magnitude
local TravelTime = Distance / enemy.Humanoid.WalkSpeed
local MoveAnimation = TS:Create(enemy:WaitForChild("HumanoidRootPart"), TweenInfo.new(TravelTime, Enum.EasingStyle.Linear), {CFrame = CFrame.new(nextPoint.CFrame.X, enemy.HumanoidRootPart.CFrame.Y, nextPoint,CFrame.Z)})
MoveAnimation:Play()
MoveAnimation.Completed:Wait()
end
end