3 variables in a for loop?

Does anybody know how I could get 3 values with a for loop? I am trying to find points of a path for a tds game. I need to find the distance between the point the mob is going to and the one after that, so I can find the distance. This is my code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Module = require(ReplicatedStorage.Points)

local Humanoid = script.Parent
local Monster = Humanoid.Parent

local TweenInfo = TweenInfo.new(5, Enum.EasingStyle.Linear)

for i, V, C in pairs(Module) do
	local distance = math.abs(V:WaitForChild("Attachment").WorldPosition, C:WaitForChild("Attachment").WorldPosition).Magnitude
	print(distance)
	goal = {CFrame  = V}
	local Tween = TweenService:Create(Monster, TweenInfo, goal)
	Tween:Play()
	Tween.Completed:Wait()
end

And this is my module script(I know it’s useless to use a module script):

local points = workspace.Points

local positions = {
	points.Start,
	points.Point1,
	points.Point2,
	points.Point3,
	points.Point4,
	points.End
}

return positions

If there is a way to do this please let me know. Thanks!

Just subtract both vectors (the attachments world position) and get the magnitude. This will give you the distance between both points.

1 Like

You can’t have more than 2 values for a for loop. I assume C is the next point? You can just use the next function to get the next value:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Module = require(ReplicatedStorage.Points)

local Humanoid = script.Parent
local Monster = Humanoid.Parent

local TweenInfo = TweenInfo.new(5, Enum.EasingStyle.Linear)

for i, V in pairs(Module) do
    local i2, C = next(Module, i)
    -- i2 is the index of the next item in the list
    -- C is the value of the next item in the list

    if not i2 then
       -- current item is the last item in the list
    end

	local distance = math.abs(V:WaitForChild("Attachment").WorldPosition, C:WaitForChild("Attachment").WorldPosition).Magnitude
	print(distance)
	goal = {CFrame  = V}
	local Tween = TweenService:Create(Monster, TweenInfo, goal)
	Tween:Play()
	Tween.Completed:Wait()
end

That’s not the issue I have to get the second point.

Tried that and it still says it tried to index nil with waitforchild.

To add onto this since you already have an array with the instances stored, in your iteration to successfully get your start point and end point just make a reference to the current part in the iterations position, and a reference to the one behind it by doing i - 1 which will return the previous index or u can do it vise versa (referring to your references)

What does your new code look like? If it’s the same as what I posted, you need to modify this part:

When the loop gets to the end of the list, there won’t be a next point to get to

This:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Module = require(ReplicatedStorage.Points)

local Humanoid = script.Parent
local Monster = Humanoid.Parent

local TweenInfo = TweenInfo.new(5, Enum.EasingStyle.Linear)

for i, V in pairs(Module) do
	local C = next(Module, i)
	local distance = math.abs((V:WaitForChild("Attachment").WorldPosition, C:WaitForChild("Attachment").WorldPosition).Magnitude)
	print(distance)
	goal = {CFrame  = V}
	local Tween = TweenService:Create(Monster, TweenInfo, goal)
	Tween:Play()
	Tween.Completed:Wait()
end

But your code didn’t work either. I will try modifiying it though

Now it says

  19:27:14.980  Workspace.OrbEnemy.Humanoid.Enemy:40: invalid argument #1 to 'abs' (number expected, got Vector3)  -  Server - Enemy:40

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local TweenService = game:GetService(“TweenService”)

local Module = require(ReplicatedStorage.Points)

local Humanoid = script.Parent
local Monster = Humanoid.Parent

local TweenInfo = TweenInfo.new(5, Enum.EasingStyle.Linear)

for i, V in pairs(Module) do
local Behind = Module[i-1]
local distance = math.abs((V:WaitForChild(“Attachment”).WorldPosition -Behind:WaitForChild(“Attachment”).WorldPosition).Magnitude)
print(distance)
goal = {CFrame = V.CFrame}
local Tween = TweenService:Create(Monster, TweenInfo, goal)
Tween:Play()
Tween.Completed:Wait()
end try this

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