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!
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
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)
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
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