I would like to make a coaster follow a path of bricks using the position and rotation of the part and cframing the train along the path.
See Image below: http://i.prntscr.com/132lorrkS2Ko-c0r20eRZA.png
I have this code which allows it to be cframed around the track facing foward but it ends up derailing as there is too much pressure on the direction. Would there be anyway to make it follow the parts?
Thanks.
local Model = script.Parent for i = 0,1,.001 do Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame() *CFrame.new(0, 0,-0.5)) wait() end
First, I would name the rail parts in order of how they are placed (1 being the start). Then, I would give each rollercoaster segment an invisible PrimaryPart (that is located where the segment meets the rails), and then also name the rollercoaster segments from 1 to 4 (1 being the furthest back & 4 being the furthest forward). After this, you can CFrame each segment with this method to move it smoothly between rail parts. With all this in mind, something like this as an outline could help get you started:
local function StartCoaster(Coaster,RailModel) local Rails = RailModel:GetChildren() --sort rails table from lowest number name to highest number name: table.sort(Rails,function(a,b) return tonumber(a.Name) < tonumber(b.Name) end) for RailNum,Rail in pairs(Rails) do --go through ordered rails for _,Segment in pairs(Coaster:GetChildren()) do local GoalRail = Rails[RailNum + tonumber(Segment.Name)] TweenModel(Segment, GoalRail.CFrame) --handle tweening here end wait(TweenTime) --change to time taken for tween end end StartCoaster(workspace.Coaster, workspace.RailParts)
Inside this function, it finds the next rail for each segment (segment number + RailNum) and tweens that segment to the desired rail. As RailNum increments, the coaster advances along the rail. This isn't tested and probably has bugs, but it should help as an outline for your solution.