So I want to make a ship sail forever on the same path. I have tried this, but it does not work:
local waypoints = game.Workspace.Waypoints:GetChildren()
local ship = game.Workspace.ShipTest
local tweenservice = game:GetService("TweenService")
while true do
for i = 1,#waypoints do
local point = waypoints[i]
ship.PrimaryPart.CFrame = CFrame.lookAt(point,nextpoint)
tweenservice:Create(ship.PrimaryPart, TweenInfo.new(3), {CFrame = i.CFrame}):Play()
wait(3)
end
end
āiā is an index and not a part, so you canāt derive a CFrame from this
CFrame.lookat requires Vector3s, not base parts
You canāt tween a modelās primarypart, unfortunately. You would have to weld the ship to the primary part, anchor the primary part, and unanchor the rest of the parts.
Hereās what Iād do:
local waypoints = game.Workspace.Waypoints:GetChildren()
local ship = game.Workspace.ShipTest
local tweenservice = game:GetService("TweenService")
-- Function that welds model's parts to PrimaryPart
local function weldModel(model)
local primaryPart = model.PrimaryPart
primaryPart.Anchored = true
for _, part in model:GetDescendants() do
if part:IsA("BasePart") and part ~= primaryPart then
local weld = Instance.new("WeldConstraint", primaryPart)
weld.Part0 = primaryPart
weld.Part1 = part
part.Anchored = false
end
end
end
weldModel(ship)
while true do
for i = 1, #waypoints do
local point = waypoints[i]
-- If ship is going through last loop, set the nextpoint to 1 for the beginning
-- Otherwise, add 1 from the point
local nextindex = i == #waypoints and 1 or i+1
local nextpoint = waypoints[nextindex]
-- Face the ship from point's position to nextpoint's position
ship.PrimaryPart.CFrame = CFrame.lookAt(point.Position, nextpoint.Position)
-- Tween to nextpoint's CFrame
tweenservice:Create(ship.PrimaryPart, TweenInfo.new(3), {CFrame = nextpoint.CFrame}):Play()
wait(3)
end
end
I actually havenāt tested this, so let me know if anything comes up
Ohhhh!!! I understood a bit more now, thank you. It still has some issues like, it rotates a bit weird and it doesnāt go through the waypoints in order. Iām not sure how Iāll work it out but I might manage.
The reason itās out of order is :GetChildren() does not return the children in any specific order.
Once youāve retrieved these children and stored them in your waypoints variable, you can sort them yourself like so: