I’m looking for the rail cart to stop rotating. I’ve realized that it’s because of the base orientation of the part I’m using as positional data along the tracks.
SETUP
Each track segment has an end and start part to signify positional data. The tracks are able to be place within a grid in whatever order (including reversed). When it’s reversed though, it will rotate the cart.
I’ve tried fixing this by setting the goal to position instead of CFrame. When I did this though, it decided to use the world position even after I had it print the Vector3 before playing the tween.
robloxapp-20241227-2202350.wmv (598.9 KB)
The very end is how I intend it to be and those segments are using the proper orientation for the start and end segments.
Here is a close up an individual track.
And finally, here is the code. This isn’t intended to be my finished product, so for now this is a server script within the cart model. A folder holds all the track segments.
TweenService = game:GetService("TweenService")
Cart = script.Parent.Parent.Center
MovementTarget = {
Parent = nil,
Target = nil,
POS = nil,
LookAtTarget = nil,
Type = nil,
Previous = nil
}
AlreadyTouching = false
CartSpeed = 5 --Amount of time it will take to get to each point
Cart_Speed_Degradation = 0.1 -- Will add this amount of time each track that the cart passes
Cart_Boost = 0.5 --
Finished_Moving = true
--Create movement function for easy & consistent recall
function Move()
print("Moving Cart...")
Finished_Moving = false
Cart.Anchored = true
local Cart_Movement_Speed = CartSpeed
if MovementTarget.Type == "Turn" then
Cart_Movement_Speed = Cart_Movement_Speed/2
CartSpeed = CartSpeed + (Cart_Speed_Degradation/2)
elseif MovementTarget.Type == "Boost" then
Cart_Movement_Speed = Cart_Movement_Speed*Cart_Boost
CartSpeed = CartSpeed*Cart_Boost
else
Cart_Movement_Speed = Cart_Movement_Speed + Cart_Speed_Degradation
CartSpeed = CartSpeed + Cart_Speed_Degradation
end
local tweenInfo = TweenInfo.new(Cart_Movement_Speed,Enum.EasingStyle.Linear)
print("Moving to: "..tostring(MovementTarget.POS))
local Goal = {CFrame = CFrame.new(MovementTarget.POS)}
local Move_Cart_Now = TweenService:Create(Cart, tweenInfo, Goal)
Move_Cart_Now:Play()
Move_Cart_Now.Completed:Connect(function()
Finished_Moving = true
Cart:PivotTo(CFrame.new(MovementTarget.POS,Vector3.new(0,0,0)))
Cart.Anchored = false
end)
end
--Update Movement data function
function Update_Movement_Data(OriginalParent,Data)
MovementTarget.Target = OriginalParent.Parent:FindFirstChild(Data).Name
MovementTarget.POS = OriginalParent.Parent:FindFirstChild(Data).Position
Cart.CFrame = CFrame.new(OriginalParent.Position,MovementTarget.POS)
MovementTarget.Type = OriginalParent.Parent.Parent:FindFirstChild("Type").Value
Move()
end
--Classify functions called when parts are touched to continue track navigation (Should dynamically add the next target marker when touched!)
for i,v in pairs(workspace.Rail_Cart_Tracks:GetChildren()) do
for int,val in pairs(v.Movement_Control:GetChildren()) do
val.Touched:Connect(function(Touched)
if AlreadyTouching ~= true then
AlreadyTouching = true
if Finished_Moving == true and Touched.Name == Cart.Name then
print("Touched and activated by: "..val.Parent.Parent.Name)
if val.Name == "1" and val.Parent:FindFirstChild("2") ~= nil then
Update_Movement_Data(val,"2")
elseif val.Name == "2" and val.Parent:FindFirstChild("3") ~= nil then
Update_Movement_Data(val,"3")
elseif val.Name == "2" and val.Parent:FindFirstChild("1") ~= nil then
Update_Movement_Data(val,"1")
elseif val.Name == "3" and val.Parent:FindFirstChild("2") ~= nil then
Update_Movement_Data(val,"2")
end
val:Destroy()
end
AlreadyTouching = false
end
end)
end
end
--Short countdown until minecart starts
for i = 1,0,-1 do
print("Starting Minecart in: "..tostring(i))
wait(1)
end
MovementTarget.POS = workspace.Rail_Cart_Tracks:FindFirstChild("1").Movement_Control:FindFirstChild("1").Position
Move()