Advertisement
Guest User

Track creation code

a guest
May 6th, 2021
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.24 KB | None | 0 0
  1. -- Creates tracks for wheels and their hinges
  2.  
  3. if Stats.TrackInfo.EnableTracks.Value == true then
  4.     local LeftWheel = 1
  5.     local RightWheel = 1
  6.     local TracksFolder = Instance.new("Model")
  7.     TracksFolder.Name = "Tracks"
  8.     TracksFolder.Parent = script.Parent.Parent
  9.     local TrackData = Stats.TrackInfo
  10.     for i,v in pairs(Wheels.LeftWheels:GetChildren()) do
  11.         local Wheel1 = Wheels.LeftWheels:FindFirstChild("Left"..tostring(LeftWheel))
  12.         local Wheel2 = Wheels.LeftWheels:FindFirstChild("Left"..tostring(LeftWheel+1))
  13.        
  14.         if Wheel1 and Wheel2 then
  15.             local Track = Instance.new("Part")
  16.             local WheelDistance = (Wheel1.TrackConnection.Position - Wheel2.TrackConnection.Position).Magnitude
  17.             Track.Size = Vector3.new(WheelDistance+0.2, TrackData.Thickness.Value, Wheel1.WheelWeld.Size.X + TrackData.WidthModifier.Value)
  18.             local TrackPos = Wheel1.TrackConnection.Position + 0.5*(Wheel2.TrackConnection.Position - Wheel1.TrackConnection.Position)
  19.             local TrackVector = (Wheel1.TrackConnection.Position - Wheel2.TrackConnection.Position).Unit:Cross(Vector3.new(0,1,0))
  20.             Track.CFrame = CFrame.new(TrackPos, TrackPos+TrackVector)
  21.             Track.Parent = TracksFolder
  22.             Track.Color = TrackData.Color.Value
  23.             Track.Material = TrackData.Material.Value
  24.             Track.CanCollide = false
  25.            
  26.             -- Creates hinges
  27.             local RearAttachment = Instance.new("Attachment")
  28.             RearAttachment.Parent = Track
  29.             RearAttachment.Name = "RearAttachment"
  30.             RearAttachment.Position = Vector3.new(Track.Size.X/2, Track.Size.Y/2, 0)
  31.             RearAttachment.Orientation = Vector3.new(0,-90,0)
  32.            
  33.             local FrontAttachment = Instance.new("Attachment")
  34.             FrontAttachment.Parent = Track
  35.             FrontAttachment.Name = "FrontAttachment"
  36.             FrontAttachment.Position = Vector3.new(-Track.Size.X/2, Track.Size.Y/2, 0)
  37.             FrontAttachment.Orientation = Vector3.new(0,-90,0)
  38.            
  39.             local RearAttachmentWheel = Instance.new("Attachment")
  40.             RearAttachmentWheel.Parent = Wheel2.TrackConnection
  41.             RearAttachmentWheel.Name = "RearAttachment"
  42.             RearAttachmentWheel.WorldPosition = RearAttachment.WorldPosition
  43.            
  44.             local FrontAttachmentWheel = Instance.new("Attachment")
  45.             FrontAttachmentWheel.Parent = Wheel1.TrackConnection
  46.             FrontAttachmentWheel.Name = "FrontAttachment"
  47.             FrontAttachmentWheel.WorldPosition = FrontAttachment.WorldPosition
  48.            
  49.             local FrontHinge = Instance.new("HingeConstraint")
  50.             FrontHinge.Name = "FrontHinge"
  51.             FrontHinge.Parent = Wheel1.TrackConnection
  52.             FrontHinge.Attachment0 = FrontAttachmentWheel
  53.             FrontHinge.Attachment1 = FrontAttachment
  54.            
  55.             local RearHinge = Instance.new("HingeConstraint")
  56.             RearHinge.Name = "RearHinge"
  57.             RearHinge.Parent = Wheel2.TrackConnection
  58.             RearHinge.Attachment0 = RearAttachmentWheel
  59.             RearHinge.Attachment1 = RearAttachment
  60.        
  61.         end
  62.         LeftWheel = LeftWheel+1
  63.     end
  64.     for i,v in pairs(Wheels.RightWheels:GetChildren()) do
  65.         local Wheel1 = Wheels.RightWheels:FindFirstChild("Right"..tostring(RightWheel))
  66.         local Wheel2 = Wheels.RightWheels:FindFirstChild("Right"..tostring(RightWheel+1))
  67.         if Wheel1 and Wheel2 then
  68.             local Track = Instance.new("Part")
  69.             local WheelDistance = (Wheel1.TrackConnection.Position - Wheel2.TrackConnection.Position).Magnitude
  70.             Track.Size = Vector3.new(WheelDistance, TrackData.Thickness.Value, Wheel1.WheelWeld.Size.X + TrackData.WidthModifier.Value)
  71.             local TrackPos = Wheel1.TrackConnection.Position + 0.5*(Wheel2.TrackConnection.Position - Wheel1.TrackConnection.Position)
  72.             local TrackVector = (Wheel1.TrackConnection.Position - Wheel2.TrackConnection.Position).Unit:Cross(Vector3.new(0,1,0))
  73.             Track.CFrame = CFrame.new(TrackPos, TrackPos+TrackVector)
  74.             Track.Parent = TracksFolder
  75.             Track.Color = TrackData.Color.Value
  76.             Track.Material = TrackData.Material.Value
  77.             Track.CanCollide = false
  78.            
  79.             -- Creates hinges
  80.             local RearAttachment = Instance.new("Attachment")
  81.             RearAttachment.Parent = Track
  82.             RearAttachment.Name = "RearAttachment"
  83.             RearAttachment.Position = Vector3.new(Track.Size.X/2, Track.Size.Y/2, 0)
  84.             RearAttachment.Orientation = Vector3.new(0,-90,0)
  85.    
  86.             local FrontAttachment = Instance.new("Attachment")
  87.             FrontAttachment.Parent = Track
  88.             FrontAttachment.Name = "FrontAttachment"
  89.             FrontAttachment.Position = Vector3.new(-Track.Size.X/2, Track.Size.Y/2, 0)
  90.             FrontAttachment.Orientation = Vector3.new(0,-90,0)
  91.    
  92.             local RearAttachmentWheel = Instance.new("Attachment")
  93.             RearAttachmentWheel.Parent = Wheel2.TrackConnection
  94.             RearAttachmentWheel.Name = "RearAttachment"
  95.             RearAttachmentWheel.WorldPosition = RearAttachment.WorldPosition
  96.    
  97.             local FrontAttachmentWheel = Instance.new("Attachment")
  98.             FrontAttachmentWheel.Parent = Wheel1.TrackConnection
  99.             FrontAttachmentWheel.Name = "FrontAttachment"
  100.             FrontAttachmentWheel.WorldPosition = FrontAttachment.WorldPosition
  101.    
  102.             local FrontHinge = Instance.new("HingeConstraint")
  103.             FrontHinge.Name = "FrontHinge"
  104.             FrontHinge.Parent = Wheel1.TrackConnection
  105.             FrontHinge.Attachment0 = FrontAttachmentWheel
  106.             FrontHinge.Attachment1 = FrontAttachment
  107.    
  108.             local RearHinge = Instance.new("HingeConstraint")
  109.             RearHinge.Name = "RearHinge"
  110.             RearHinge.Parent = Wheel2.TrackConnection
  111.             RearHinge.Attachment0 = RearAttachmentWheel
  112.             RearHinge.Attachment1 = RearAttachment
  113.    
  114.         end
  115.         RightWheel = RightWheel+1
  116.     end
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement