HowToRoblox

DealershipServer

Jun 27th, 2022 (edited)
837
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.53 KB | None
  1. --Create folder to contain spawned cars
  2. local spawnedCars = Instance.new("Folder")
  3. spawnedCars.Name = "SpawnedCars"
  4. spawnedCars.Parent = workspace
  5.  
  6.  
  7. --Save data when player leaves
  8. local dss = game:GetService("DataStoreService")
  9. local ds = dss:GetDataStore("CarsDataStore")
  10.  
  11. function saveData(player)
  12.    
  13.     local cash = player.leaderstats.Cash.Value
  14.    
  15.     local cars = {}
  16.    
  17.     for i, car in pairs(player.Cars:GetChildren()) do
  18.         table.insert(cars, car.Name)
  19.     end
  20.    
  21.     ds:SetAsync(player.UserId .. "Cash", cash)
  22.     ds:SetAsync(player.UserId .. "Cars", cars)
  23. end
  24.  
  25. game.Players.PlayerRemoving:Connect(saveData)
  26. game:BindToClose(function()
  27.     for i, player in pairs(game.Players:GetPlayers()) do
  28.         saveData(player)
  29.     end
  30. end)
  31.  
  32.  
  33. --Load data when player joins
  34. game.Players.PlayerAdded:Connect(function(player)
  35.    
  36.     local leaderstats = Instance.new("Folder")
  37.     leaderstats.Name = "leaderstats"
  38.     leaderstats.Parent = player
  39.    
  40.     local cash = Instance.new("IntValue")
  41.     cash.Name = "Cash"
  42.     cash.Parent = leaderstats
  43.    
  44.     local carsFolder = Instance.new("Folder")
  45.     carsFolder.Name = "Cars"
  46.     carsFolder.Parent = player
  47.    
  48.    
  49.     local cashData = ds:GetAsync(player.UserId .. "Cash") or 100000
  50.     local carsData = ds:GetAsync(player.UserId .. "Cars") or {}
  51.    
  52.     cash.Value = cashData
  53.    
  54.     for i, car in pairs(carsData) do
  55.         if game.ReplicatedStorage.Cars:FindFirstChild(car) then
  56.             game.ReplicatedStorage.Cars:FindFirstChild(car):Clone().Parent = carsFolder
  57.         end
  58.     end
  59. end)
  60.  
  61.  
  62. --Buying and spawning of cars
  63. game.ReplicatedStorage:WaitForChild("CarsRE").OnServerEvent:Connect(function(player, instruction, car, dealership)
  64.    
  65.     local carRequest = game.ReplicatedStorage.Cars:FindFirstChild(car)
  66.     if carRequest then
  67.        
  68.         if instruction == "BUY" then
  69.             if player.leaderstats.Cash.Value >= carRequest.PRICE.Value and not player.Cars:FindFirstChild(carRequest.Name) then
  70.                
  71.                 player.leaderstats.Cash.Value -= carRequest.PRICE.Value
  72.                 carRequest:Clone().Parent = player.Cars
  73.             end
  74.            
  75.         elseif instruction == "SPAWN" then
  76.             if player.Cars:FindFirstChild(carRequest.Name) then
  77.                
  78.                 if spawnedCars:FindFirstChild(player.Name) then
  79.                     spawnedCars[player.Name]:Destroy()
  80.                     for i, desc in pairs(workspace.CarDealerships:GetDescendants()) do
  81.                         if desc.Name == player.Name and desc.Parent.Parent.Name == "CarSpawns" then
  82.                             desc:Destroy()
  83.                         end
  84.                     end
  85.                 end
  86.                
  87.                 local spawnedCar = carRequest:Clone()
  88.                 spawnedCar.Name = player.Name
  89.                
  90.                 local chosenSpawn = nil
  91.                 for i, carSpawn in pairs(dealership.CarSpawns:GetChildren()) do
  92.                     if #carSpawn:GetChildren() < 1 then
  93.                         chosenSpawn = carSpawn
  94.                         local takenValue = Instance.new("StringValue")
  95.                         takenValue.Name = player.Name
  96.                         takenValue.Parent = chosenSpawn
  97.                         break
  98.                     end
  99.                 end
  100.                
  101.                 spawnedCar:SetPrimaryPartCFrame(chosenSpawn.CFrame)
  102.                
  103.                 spawnedCar.Parent = spawnedCars
  104.             end
  105.         end
  106.     end
  107. end)
  108.  
  109.  
  110. --Removing cars when player leaves
  111. game.Players.PlayerRemoving:Connect(function(player)
  112.     if spawnedCars:FindFirstChild(player.Name) then
  113.         spawnedCars[player.Name]:Destroy()
  114.         for i, desc in pairs(workspace.CarDealerships:GetDescendants()) do
  115.             if desc.Name == player.Name and desc.Parent.Parent.Name == "CarSpawns" then
  116.                 desc:Destroy()
  117.             end
  118.         end
  119.     end
  120. end)
  121.  
  122.  
  123. --Set up dealerships
  124. for i, dealership in pairs(workspace.CarDealerships:GetChildren()) do
  125.    
  126.     local prompt = Instance.new("ProximityPrompt")
  127.     prompt.ObjectText = "Car Dealership"
  128.     prompt.ActionText = "Hold E to check cars"
  129.     prompt.HoldDuration = 1
  130.     prompt.Parent = dealership.PromptHolder
  131.    
  132.     prompt.Triggered:Connect(function(player)
  133.         game.ReplicatedStorage.CarsRE:FireClient(player, prompt)
  134.     end)
  135.    
  136.     local cars = game.ReplicatedStorage.Cars:GetChildren()
  137.    
  138.     for x, platform in pairs(dealership.CarPlatforms:GetChildren()) do
  139.        
  140.         local randomNum = math.random(1, #cars)
  141.         local randomCar = cars[randomNum]
  142.         table.remove(cars, randomNum)
  143.        
  144.         randomCar = randomCar:Clone()
  145.        
  146.         for y, desc in pairs(randomCar:GetDescendants()) do
  147.            
  148.             if desc:IsA("Seat") or desc:IsA("Script") or desc:IsA("VehicleSeat") or desc:IsA("LocalScript") then
  149.                 desc:Destroy()
  150.             end
  151.             if desc:IsA("BasePart") then
  152.                 desc.Anchored = true
  153.             end
  154.         end
  155.        
  156.         randomCar:SetPrimaryPartCFrame(CFrame.new(platform.Position, -randomCar.CameraPosition.CFrame.LookVector * 10000))
  157.         randomCar.Parent = platform
  158.        
  159.         spawn(function()
  160.             while wait() do
  161.                 randomCar:SetPrimaryPartCFrame(randomCar.PrimaryPart.CFrame * CFrame.Angles(0, 0.005, 0))
  162.             end
  163.         end)
  164.     end
  165. end
RAW Paste Data Copied