Advertisement
HowToRoblox

CTPServer

Dec 2nd, 2022
1,309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.51 KB | None | 0 0
  1. --Data handling
  2. local dss = game:GetService("DataStoreService")
  3. local ds = dss:GetDataStore("DATA STORES")
  4.  
  5.  
  6. function saveData(plr)
  7.  
  8.     if not plr:FindFirstChild("FAILED TO LOAD DATA") then
  9.         local cash = plr.leaderstats.Cash.Value
  10.  
  11.         local success, err = nil, nil
  12.         while not success do
  13.             success, err = pcall(function()
  14.                 return ds:SetAsync(plr.UserId, cash)
  15.             end)
  16.             warn(err)
  17.             task.wait(0.1)
  18.         end
  19.     end
  20. end
  21.  
  22. game.Players.PlayerRemoving:Connect(saveData)
  23. game:BindToClose(function()
  24.     for i, plr in pairs(game.Players:GetPlayers()) do
  25.         saveData(plr)
  26.     end
  27. end)
  28.  
  29.  
  30. game.Players.PlayerAdded:Connect(function(plr)
  31.  
  32.     local ls = Instance.new("Folder")
  33.     ls.Name = "leaderstats"
  34.     ls.Parent = plr
  35.  
  36.     local cashValue = Instance.new("IntValue")
  37.     cashValue.Name = "Cash"
  38.     cashValue.Parent = ls
  39.  
  40.  
  41.     local success, data = pcall(function()
  42.         return ds:GetAsync(plr.UserId)
  43.     end)
  44.  
  45.     if success then
  46.         cashValue.Value = data or 0
  47.  
  48.     else
  49.         local failedValue = Instance.new("StringValue")
  50.         failedValue.Name = "FAILED TO LOAD DATA"
  51.         failedValue.Parent = plr
  52.     end
  53. end)
  54.  
  55.  
  56. --Main game loop
  57. local rs = game.ReplicatedStorage:WaitForChild("CTPReplicatedStorage")
  58. local config = require(rs:WaitForChild("CONFIGURATION"))
  59. local maps = rs:WaitForChild("Maps")
  60. local weapons = rs:WaitForChild("Weapons")
  61.  
  62. local statusValue = Instance.new("StringValue")
  63. statusValue.Name = "STATUS"
  64. statusValue.Parent = rs
  65.  
  66.  
  67. while true do
  68.  
  69.     local plrsInGame = {}
  70.     while true do
  71.         plrsInGame = {}
  72.         for i, plr in pairs(game.Players:GetPlayers()) do
  73.             if plr.Character and plr.Character.Humanoid.Health > 0 then
  74.                 table.insert(plrsInGame, plr)
  75.             end
  76.         end
  77.  
  78.         if #plrsInGame >= config.MinimumPlayersRequired then
  79.             break
  80.         else
  81.             local neededPlayers = config.MinimumPlayersRequired - #plrsInGame
  82.             statusValue.Value = "Waiting for " .. neededPlayers .. " more player" .. (neededPlayers ~= 1 and "s" or "")
  83.  
  84.             task.wait(1)
  85.         end
  86.     end
  87.  
  88.     statusValue.Value = "Intermission"
  89.     task.wait(config.IntermissionTime)
  90.  
  91.     local chosenMap = maps:GetChildren()[Random.new():NextInteger(1, #maps:GetChildren())]:Clone()
  92.     chosenMap.Parent = workspace
  93.  
  94.     statusValue.Value = "Now entering " .. chosenMap.Name
  95.  
  96.     task.wait(config.MapWaitTime)
  97.  
  98.  
  99.     for i, plr in pairs(plrsInGame) do
  100.         local chosenTeam = i%2==1 and "Team1" or "Team2"
  101.         plr.Team = config[chosenTeam]
  102.  
  103.         for x, tool in pairs(weapons[chosenTeam .. "Weapons"]:GetChildren()) do
  104.             tool:Clone().Parent = plr.Backpack
  105.         end
  106.  
  107.         local char = plr.Character
  108.         char.HumanoidRootPart.CFrame = chosenMap.PlayerSpawns[chosenTeam .. "Spawn"].CFrame
  109.  
  110.         local connection = plr.CharacterAdded:Connect(function(newChar)
  111.             local newHRP = newChar:WaitForChild("HumanoidRootPart")
  112.             newHRP:GetPropertyChangedSignal("CFrame"):Wait()
  113.             newHRP.CFrame = chosenMap.PlayerSpawns[chosenTeam .. "Spawn"].CFrame
  114.  
  115.             for x, tool in pairs(weapons[chosenTeam .. "Weapons"]:GetChildren()) do
  116.                 tool:Clone().Parent = plr.Backpack
  117.             end
  118.         end)
  119.  
  120.         chosenMap.Destroying:Connect(function()
  121.             connection:Disconnect()
  122.         end)
  123.     end
  124.    
  125.    
  126.     local scoresFolder = Instance.new("Folder")
  127.     scoresFolder.Name = "SCORES"
  128.     scoresFolder.Parent = rs
  129.  
  130.     local team1Score = Instance.new("IntValue")
  131.     team1Score.Name = "TEAM 1 SCORE"
  132.     team1Score.Parent = scoresFolder
  133.     local team2Score = Instance.new("IntValue")
  134.     team2Score.Name = "TEAM 2 SCORE"
  135.     team2Score.Parent = scoresFolder
  136.    
  137.     local contestedValue = Instance.new("NumberValue")
  138.     contestedValue.Name = ""
  139.     contestedValue.Parent = scoresFolder
  140.  
  141.    
  142.     local point = chosenMap:WaitForChild("Point"):WaitForChild("Point")
  143.     point.Color = Color3.fromRGB(168, 168, 168)
  144.    
  145.     local charactersOnPoint = {}
  146.  
  147.     local gameStart = tick()
  148.    
  149.     local timePerPoint = 1 / config.PointsPerSecond
  150.     local lastPointGiven = 0
  151.    
  152.     local pointTakenBy = nil
  153.     local lastPointContested = nil
  154.    
  155.     while true do
  156.         game:GetService("RunService").Heartbeat:Wait()
  157.  
  158.         local timeLeft = config.RoundTime - math.round((tick() - gameStart))
  159.         local mins = math.floor(timeLeft / 60)
  160.         local secs = timeLeft - (mins * 60)
  161.         if string.len(secs) < 2 then
  162.             secs = "0" .. tostring(secs)
  163.         end
  164.         statusValue.Value = mins .. ":" .. secs
  165.        
  166.        
  167.         for i, plr in pairs(plrsInGame) do
  168.             local char = plr.Character
  169.             if char.Humanoid.Health > 0 and (char.HumanoidRootPart.Position * Vector3.new(1, 0, 1) - point.Position * Vector3.new(1, 0, 1)).Magnitude <= point.Size.Z/2 then
  170.                 if not table.find(charactersOnPoint, char) then
  171.                     table.insert(charactersOnPoint, char)
  172.                 end
  173.             elseif table.find(charactersOnPoint, char) then
  174.                 table.remove(charactersOnPoint, table.find(charactersOnPoint, char))
  175.             end
  176.         end
  177.        
  178.        
  179.         local numTeam1 = 0
  180.         local numTeam2 = 0
  181.         for i, charOnPoint in pairs(charactersOnPoint) do
  182.             local plr = game.Players:GetPlayerFromCharacter(charOnPoint)
  183.             if plr then
  184.                 if plr.Team == config.Team1 then
  185.                     numTeam1 += 1
  186.                 elseif plr.Team == config.Team2 then
  187.                     numTeam2 += 1
  188.                 end
  189.             end
  190.         end
  191.        
  192.         if numTeam1 > 0 and numTeam2 == 0 then
  193.             if not lastPointContested or lastPointContested[1] ~= "Team1" then
  194.                 lastPointContested = {"Team1", tick()}
  195.             elseif lastPointContested[1] == "Team1" then
  196.                 local timeContested = tick() - lastPointContested[2]
  197.                 if timeContested >= config.TimeToCapture then
  198.                     pointTakenBy = "Team1"
  199.                 end
  200.             end
  201.            
  202.         elseif numTeam2 > 0 and numTeam1 == 0 then
  203.             if not lastPointContested or lastPointContested[1] ~= "Team2" then
  204.                 lastPointContested = {"Team2", tick()}
  205.             elseif lastPointContested[1] == "Team2" then
  206.                 local timeContested = tick() - lastPointContested[2]
  207.                 if timeContested >= config.TimeToCapture then
  208.                     pointTakenBy = "Team2"
  209.                 end
  210.             end
  211.            
  212.         elseif (numTeam1 ~= 0 and numTeam2 ~= 0) or (numTeam1 == 0 and numTeam2 == 0) then
  213.             lastPointContested = nil
  214.         end
  215.        
  216.         if lastPointContested then
  217.             contestedValue.Name = lastPointContested[1]
  218.             contestedValue.Value = tick() - lastPointContested[2]
  219.         else
  220.             contestedValue.Name = ""
  221.             contestedValue.Value = 0
  222.         end
  223.        
  224.        
  225.         if pointTakenBy then
  226.             point.Color = config[pointTakenBy].TeamColor.Color
  227.            
  228.             if tick() - lastPointGiven >= timePerPoint then
  229.                 if pointTakenBy == "Team1" then
  230.                     team1Score.Value += 1
  231.                 elseif pointTakenBy == "Team2" then
  232.                     team2Score.Value += 1
  233.                 end
  234.                 lastPointGiven = tick()
  235.             end
  236.         else
  237.             point.Color = Color3.fromRGB(168, 168, 168)
  238.         end
  239.  
  240.         numPlrs = 0
  241.         for i, plr in pairs(game.Players:GetPlayers()) do
  242.             if plr then
  243.                 numPlrs += 1
  244.             end
  245.         end
  246.         if numPlrs < config.MinimumPlayersRequired then
  247.             break
  248.  
  249.         elseif config.StopOncePointsReached then
  250.             if team1Score.Value >= config.PointsRequiredToStop or team2Score.Value >= config.PointsRequiredToStop then
  251.                 break
  252.             end
  253.  
  254.         elseif timeLeft <= 0 then
  255.             break
  256.         end
  257.     end
  258.  
  259.     if team1Score.Value > team2Score.Value then
  260.         statusValue.Value = "Team " .. config.Team1.Name .. " wins!"
  261.  
  262.         for i, plr in pairs(plrsInGame) do
  263.             if plr.Team == config.Team1 then
  264.                 plr.leaderstats.Cash.Value += config.WinReward
  265.             end
  266.         end
  267.  
  268.     elseif team2Score.Value > team1Score.Value then
  269.         statusValue.Value = "Team " .. config.Team2.Name .. " wins!"
  270.  
  271.         for i, plr in pairs(plrsInGame) do
  272.             if plr.Team == config.Team2 then
  273.                 plr.leaderstats.Cash.Value += config.WinReward
  274.             end
  275.         end
  276.  
  277.     else
  278.         statusValue.Value = "Draw!"
  279.     end
  280.  
  281.     for i, plr in pairs(plrsInGame) do
  282.         plr.Team = nil
  283.         plr:LoadCharacter()
  284.     end
  285.  
  286.     scoresFolder:Destroy()
  287.     chosenMap:Destroy()
  288.  
  289.     task.wait(config.RoundFinishedTime)
  290. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement