There is a folder in workspace called PathFolder which has all the track models, and the track model primary part is the main black stone under the rail
the black part is the primary part, it is a part not a mesh btw
And when the train moves, it should keep the train in center and it should go front
But its going like this
The train is stopping as you see and is jumping, I don’t know why it is stopping or not aligning in the middle of track
The script please help
spawn(function()
task.wait(1)
local raycastparms = RaycastParams.new()
raycastparms.FilterDescendantsInstances = workspace.PathFolder:GetChildren()
raycastparms.FilterType = Enum.RaycastFilterType.Whitelist
while wait(0.05) do
if script.Parent.Occupant ~= nil then
local multipler = script.Parent.Throttle
currentspeed += 0.07*multipler
currentspeed = math.clamp(currentspeed,0,maxspeed)
local raycastresult = workspace:Raycast(script.Parent.Parent.BasePartMoving.Position + script.Parent.Parent.BasePartMoving.CFrame.LookVector*(currentspeed/2),Vector3.new(0,-150,0),raycastparms)
if raycastresult ~= nil then
if raycastresult.Instance.Parent.Name == "Track" then
local trackmodel = raycastresult.Instance.Parent
local objectcframe = script.Parent.Parent.BasePartMoving.CFrame:ToObjectSpace(trackmodel.PrimaryPart.CFrame)
-- objectcframe is the cframe of train base with the track as the center of world
if currentblock == nil then
currentblock = trackmodel.PrimaryPart
local pos = objectcframe.Position.Z
currentz = trackmodel.PrimaryPart.Size.Z/2
elseif trackmodel ~= currentblock.Parent then
currentblock = trackmodel.PrimaryPart
currentz = (trackmodel.PrimaryPart.Size.Z/2) - (currentspeed)
end
local targetcframe = currentblock.CFrame + currentblock.CFrame.LookVector*currentz + Vector3.new(0,4,0)
script.Parent.Parent.BasePartMoving.AlignPosition.Position = targetcframe.Position
script.Parent.Parent.BasePartMoving.AlignOrientation.CFrame = targetcframe
currentz -= currentspeed
print(math.round(currentspeed/2))
end
end
end
end
end)
Thats all, please help I am new to object and world space