Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. --local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local gameGravity = game.Workspace.Gravity
  3. local thisVehicle = script.Parent.Parent.ShipModel:WaitForChild("ShipHull")
  4. local vehicleSeat = script.Parent.Parent.ShipModel:WaitForChild("VehicleSeat")
  5. local vehicleHull = script.Parent.Parent.ShipModel:WaitForChild("ShipHull")
  6. local playerVehicleControls = script.Parent:WaitForChild("PlayerVehicleControls")
  7. local scriptClone = nil
  8. local thisPlayer = nil
  9. local ControlEvent = nil
  10. local massValue = 0
  11. local massModifier = 0
  12.  
  13. --//Setup Attachments
  14. local frontHullAttachment = Instance.new("Attachment")
  15. frontHullAttachment.Parent = thisVehicle
  16. frontHullAttachment.Name = "FrontHullAttachment"
  17. frontHullAttachment.Position = Vector3.new(0,-2.5,-4)
  18. local engineTrailAttachment0 = Instance.new("Attachment")
  19. engineTrailAttachment0.Parent = thisVehicle
  20. engineTrailAttachment0.Name = "EngineTrailAttachment0"
  21. engineTrailAttachment0.Position = Vector3.new(1,-3.5,4)
  22. local engineTrailAttachment1 = Instance.new("Attachment")
  23. engineTrailAttachment1.Parent = thisVehicle
  24. engineTrailAttachment1.Name = "EngineTrailAttachment1"
  25. engineTrailAttachment1.Position = Vector3.new(-1,-3.5,4)
  26.  
  27. --// Create starting objects inside ship
  28. local BodyForce = Instance.new("BodyForce")
  29. BodyForce.Parent = script.Parent.Parent.ShipModel:WaitForChild("ShipHull")
  30. BodyForce.Name = "BodyForce"
  31. local bodyVelocity = Instance.new("BodyVelocity")
  32. bodyVelocity.Parent = script.Parent.Parent.ShipModel:WaitForChild("ShipHull")
  33. bodyVelocity.Name = "BodyVelocity"
  34. local bodyGyro = Instance.new("BodyGyro")
  35. bodyGyro.Parent = script.Parent.Parent.ShipModel:WaitForChild("ShipHull")
  36. bodyGyro.Name = "BodyGyro"
  37. local engineTrail = Instance.new("Trail")
  38. engineTrail.Parent = script.Parent.Parent.ShipModel:WaitForChild("ShipHull")
  39. engineTrail.Name = "EngineTrail"
  40. engineTrail.Attachment0 = engineTrailAttachment0
  41. engineTrail.Attachment1 = engineTrailAttachment1
  42. engineTrail.FaceCamera = true
  43.  
  44. --// Main function when player sits in seat
  45. vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
  46.     if vehicleSeat.Occupant == nil then
  47.         ExitShip()
  48.         print ("out of ship")
  49.     else
  50.         WeldAll()
  51.         CalculateMass()
  52.         NegateGravity()
  53.         SetupControls()
  54.         print ("in ship")
  55.     end
  56. end)
  57.  
  58. --// Negate Gravity using BodyForce
  59. function NegateGravity()
  60.     BodyForce.Force = Vector3.new(0, (massValue * gameGravity), 0)
  61. end
  62.  
  63.  
  64. --// Calculate mass of all parts and un-anchor them
  65. function CalculateMass()
  66.     local massCalc = 0
  67.     local shipParts = script.Parent.Parent.ShipModel:GetChildren()
  68.     for i,v in pairs(shipParts) do
  69.         if v:IsA ("BasePart") then
  70.             v.Anchored = false
  71.             massCalc = massCalc + v:GetMass()
  72.         end
  73.     end
  74.     massCalc = massCalc + massModifier
  75.     massValue = massCalc
  76.     print (massValue)
  77. end
  78.  
  79. --// Welder functions
  80. function partWeld(firstPart,secondPart)
  81.     local Part0 = firstPart
  82.     local Part1 = secondPart
  83.     local weld = Instance.new("WeldConstraint")
  84.     weld.Parent = game.Workspace
  85.     weld.Part0 = firstPart
  86.     weld.Part1 = secondPart
  87.     --print ("welded " .. tostring(firstPart) .. " and " .. tostring(secondPart))
  88. end
  89.  
  90. --// Welds all the parts otgether, this is not an auto-welder, you set the welds up manually below
  91. function WeldAll()
  92.     partWeld(vehicleHull,vehicleSeat)
  93. end
  94.  
  95.  
  96.  
  97. --// sets up all of the control events and related stuff
  98. function SetupControls()
  99.     --get player in the seat
  100.     thisPlayer = vehicleSeat.Occupant.Parent
  101.     --clone the PlayerControlScript and set the data value for thisVehicle
  102.     script.Parent.PlayerVehicleControls.Vehicle.Value = thisVehicle
  103.     scriptClone = playerVehicleControls:Clone()
  104.     scriptClone.Parent = thisPlayer
  105.     scriptClone.Name = "PlayerVehicleControls"
  106.     for i , v in pairs(thisVehicle:GetDescendants()) do
  107.         if v:IsA("BasePart") then
  108.             v:SetNetworkOwner(thisPlayer)
  109.         end
  110.     end
  111. end
  112.  
  113.  
  114.  
  115. function ExitShip()
  116.     BodyForce.Force = Vector3.new(0,0,0)
  117.     bodyVelocity.MaxForce = Vector3.new(0,0,0)
  118.     thisPlayer.PlayerVehicleControls:Destroy()
  119.         for i , v in pairs(thisVehicle:GetDescendants()) do
  120.         if v:IsA("BasePart") then
  121.             v:SetNetworkOwnershipAuto()
  122.         end
  123.     end
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement