Advertisement
remi_

Server Script

Jun 29th, 2021
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. --// Made by remi
  2. -- Script located inside the car to give ownership.
  3. local physicsSevice = game:GetService("PhysicsService")
  4. local playerService = game:GetService("Players")
  5. local replicatedStorage = game:GetService("ReplicatedStorage")
  6. local serverStorage = game:GetService("ServerStorage")
  7.  
  8. local control_remote = replicatedStorage.ControlRemote
  9.  
  10. local wheel_template = serverStorage.Wheel
  11.  
  12. local car = script.Parent
  13.  
  14. local body = car.Body
  15. local seat = car.Seat
  16.  
  17. local speed = car:GetAttribute("Speed")
  18. local vertical_speed = car:GetAttribute("VerticalSpeed")
  19. local turn_speed = car:GetAttribute("TurnSpeed")
  20. local torque = car:GetAttribute("Torque")
  21. local vertical_acceleration = car:GetAttribute("VerticalAcceleration")
  22. local acceleration = car:GetAttribute("Acceleration")
  23.  
  24. local aesthethics = car.Aesthetics
  25. local constraints = body.Constraints
  26. local physics = car.Physics
  27.  
  28. local occupant_changed = seat:GetPropertyChangedSignal("Occupant")
  29.  
  30. local enter_prompt = seat.Enter
  31.  
  32. local current_occupant = nil
  33.  
  34. local function CharacterCanCollide(character)
  35.     local collision_group = nil
  36.     local massless = false
  37.    
  38.     if physicsSevice:CollisionGroupContainsPart("NonCollide", character.PrimaryPart) then
  39.         collision_group = "Default"
  40.     else
  41.         collision_group = "NonCollide"
  42.         massless = true
  43.     end
  44.    
  45.     for _, part in pairs(character:GetChildren()) do
  46.         if part:IsA("BasePart") then
  47.             physicsSevice:SetPartCollisionGroup(part, collision_group)
  48.            
  49.             part.Massless = massless
  50.         end
  51.     end
  52. end
  53.  
  54. local function OnPlayerLeave(player)
  55.    
  56.     local character = player.Character
  57.     CharacterCanCollide(character)
  58.    
  59.     enter_prompt.Enabled = true
  60.     control_remote:FireClient(player)
  61.     body:SetNetworkOwnershipAuto()
  62.     current_occupant = nil
  63. end
  64.  
  65. local function OnPlayerEnter(player)
  66.     if current_occupant then return end
  67.    
  68.     local character = player.Character
  69.     local humanoid = character.Humanoid
  70.  
  71.     CharacterCanCollide(character)
  72.     seat:Sit(humanoid)
  73.     enter_prompt.Enabled = false
  74.    
  75.     control_remote:FireClient(player, "start", car, speed, turn_speed, torque, acceleration, vertical_speed, vertical_acceleration)
  76.     body:SetNetworkOwner(player)
  77.     current_occupant = player
  78. end
  79.  
  80. local function Init()
  81.     local wheels = physics.Wheels
  82.    
  83.     for _, physics_wheel in ipairs(wheels:GetChildren()) do
  84.         local aesthethics_wheel = wheel_template:Clone()
  85.         aesthethics_wheel.CFrame = physics_wheel.CFrame
  86.         aesthethics_wheel.Size = physics_wheel.Size
  87.         aesthethics_wheel.Parent = aesthethics
  88.        
  89.         local motor6d = Instance.new("Motor6D")
  90.         motor6d.Part0 = physics_wheel
  91.         motor6d.Part1 = aesthethics_wheel
  92.         motor6d.Parent = physics_wheel
  93.     end
  94.    
  95.     for _, constraint in ipairs(constraints:GetDescendants()) do
  96.         if constraint:IsA("CylindricalConstraint") then
  97.             constraint.MotorMaxTorque = torque
  98.         end
  99.     end
  100. end
  101.  
  102. Init()
  103. enter_prompt.Triggered:Connect(OnPlayerEnter)
  104. occupant_changed:Connect(function()
  105.     if seat.Occupant then return end
  106.     if not current_occupant then return end
  107.    
  108.     OnPlayerLeave(current_occupant)
  109. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement