Tower Defense Game; cannot get the previous waypoint [Solved]

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.
obraz_2023-08-24_233303698

If you know the solution to this please let me know.

Sorry if this was a really easy fix.

1 Like

Do you have a point called “0” inside of the waypoints folder? The error is stating that it doesn’t exist.

No, I don’t but here’s the waypoint folder
obraz_2023-08-24_235852428

That’s why. Point “0” does not exist so its trying to find something that isn’t there.

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

I was thinking that tostring() thing also … Names are strings even if you use a number.

yeah a tiny oversight but doesnt matter

im also kinda that one guy who uses string.format for almost everything

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