I’m currently in the making of a skilift type model which utilizes many waypoints positioned throughout the rope and a TweenService system to move the chair around the lift instead of bothering with Roblox Physics. I’m trying to make it no matter where the chair is placed throughout the lift, it was always find the nearest point and create a tween for itself to keep moving throughout the lift. Down below is the function that I’m using to find the nearest point in relation to the specified chair.
local function findNextPoint()
local distanceToBeat = nil
local closest
for _, Point in pairs(Chairlift.TweeningPoints:GetChildren()) do
if distanceToBeat == nil then
distanceToBeat = (Vector3.new(0, 0, Chair.PrimaryPart.Position.Z) - Vector3.new(0, 0, Point.Position.Z)).Magnitude
closest = Point
else
if (Vector3.new(0, 0, Chair.PrimaryPart.Position.Z) - Vector3.new(0, 0, Point.Position.Z)).Magnitude < distanceToBeat then
distanceToBeat = (Vector3.new(0, 0, Chair.PrimaryPart.Position.Z) - Vector3.new(0, 0, Point.Position.Z)).Magnitude
closest = Point
end
end
end
print(distanceToBeat)
print(closest.Name)
end
The problem with this script is that I only want it to find the nearest waypoint that is straight ahead of the current lift and not on the other side of the lift or behind it.
I want it to go to the green circled dot but it ends up going to the red circled dot. How can I make it cancel out all the other dots that are not straight in front of the current chair?
The issue is that the chair doesn’t have an assigned position and will vary depending on where placed. I want this function to work no matter where the chair is positioned throughout the skilift.
You can give each chair a NumberValue or a custom attribute to store which number it is currently on. The next waypoint therefore will be chairNumber + 1
That’s another thing I was trying to avoid doing, the main goal was for the script to figure it where the next waypoint is by itself. I did a little experimenting and figured out a solution. I used Vector3:Dot() to figure out where the other waypoints were facing in relation to the lift so I could cancel out all the other waypoints that were facing the opposite way (therefor meaning they were on the wrong side of the lift.)
local function findNextPoint()
local distanceToBeat = nil
local closest
for _, Point in pairs(Chairlift.TweeningPoints:GetChildren()) do
if Chair.PrimaryPart.CFrame.LookVector:Dot(Point.CFrame.LookVector) >= 0.9 then
if distanceToBeat == nil then
distanceToBeat = (Vector3.new(0, 0, Chair.PrimaryPart.Position.Z) - Vector3.new(0, 0, Point.Position.Z)).Magnitude
closest = Point
else
if (Vector3.new(0, 0, Chair.PrimaryPart.Position.Z) - Vector3.new(0, 0, Point.Position.Z)).Magnitude < distanceToBeat then
distanceToBeat = (Vector3.new(0, 0, Chair.PrimaryPart.Position.Z) - Vector3.new(0, 0, Point.Position.Z)).Magnitude
closest = Point
end
end
end
end
return({closest, distanceToBeat})
end