Advertisement
HowToRoblox

TeamDeathmatchServer

Nov 27th, 2022
2,684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.05 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local cashDataStore = dss:GetDataStore("CASH DATA STORE")
  3.  
  4.  
  5. function saveData(plr)
  6.     if not plr:FindFirstChild("FAILED TO LOAD DATA") then
  7.        
  8.         local cash = plr.leaderstats.Cash.Value
  9.        
  10.         local success, err
  11.         while not success do
  12.  
  13.             if err then
  14.                 warn(err)
  15.             end
  16.  
  17.             success, err = pcall(function()
  18.                 cashDataStore:SetAsync(plr.UserId .. "-Cash", cash)
  19.             end)
  20.             task.wait(0.5)
  21.         end
  22.     end
  23. end
  24.  
  25. game.Players.PlayerRemoving:Connect(saveData)
  26.  
  27. game:BindToClose(function()
  28.     for i, plr in pairs(game.Players:GetPlayers()) do
  29.         saveData(plr)
  30.     end
  31. end)
  32.  
  33.  
  34. game.Players.PlayerAdded:Connect(function(plr)
  35.    
  36.     local ls = Instance.new("Folder")
  37.     ls.Name = "leaderstats"
  38.     ls.Parent = plr
  39.    
  40.     local cashValue = Instance.new("IntValue")
  41.     cashValue.Name = "Cash"
  42.     cashValue.Parent = ls
  43.    
  44.    
  45.     local success, data = pcall(function()
  46.         return cashDataStore:GetAsync(plr.UserId .. "-Cash")
  47.     end)
  48.  
  49.     if success then
  50.         local cashData = data or 0
  51.         cashValue.Value = cashData
  52.  
  53.         print("Data successfully loaded for " .. plr.Name)
  54.  
  55.     else
  56.         warn("Data not loaded for " .. plr.Name)
  57.  
  58.         local failedToLoad = Instance.new("StringValue")
  59.         failedToLoad.Name = "FAILED TO LOAD DATA"
  60.         failedToLoad.Parent = plr
  61.     end
  62. end)
  63.  
  64.  
  65. local rs = game.ReplicatedStorage:WaitForChild("TeamDeathmatchReplicatedStorage")
  66. local maps = rs:WaitForChild("Maps")
  67. local weapons = rs:WaitForChild("Weapons")
  68. local config = require(rs:WaitForChild("CONFIGURATION"))
  69.  
  70. local statusValue = Instance.new("StringValue")
  71. statusValue.Name = "STATUS"
  72. statusValue.Parent = rs
  73.  
  74.  
  75. function rewardWinner(team)
  76.     statusValue.Value = "Team " .. team.Name .. " wins!"
  77.    
  78.     for i, plr in pairs(team:GetPlayers()) do
  79.         plr.leaderstats.Cash.Value += config.WinReward
  80.     end
  81. end
  82.  
  83.  
  84. while true do
  85.    
  86.     while true do
  87.        
  88.         local numChars = 0
  89.         for i, plr in pairs(game.Players:GetPlayers()) do
  90.             if plr.Character and plr.Character.Humanoid.Health > 0 then
  91.                 numChars += 1
  92.             end
  93.         end
  94.        
  95.         if numChars >= config.MinimumPlayersRequired then
  96.             break
  97.         end
  98.        
  99.         statusValue.Value = "Waiting for " .. (config.MinimumPlayersRequired - numChars) .. " more player" .. (config.MinimumPlayersRequired - numChars ~= 1 and "s." or ".")
  100.         task.wait(1)
  101.     end
  102.    
  103.     statusValue.Value = "Intermission.."
  104.     task.wait(config.IntermissionTime)
  105.    
  106.    
  107.     local team1 = config.Team1
  108.     local team2 = config.Team2
  109.    
  110.    
  111.     local mapChosen = maps:GetChildren()[Random.new():NextInteger(1, #maps:GetChildren())]:Clone()
  112.     mapChosen.Parent = workspace
  113.    
  114.     statusValue.Value = "Now playing " .. mapChosen.Name
  115.     task.wait(config.MapWaitTime)
  116.    
  117.    
  118.     local teamKillsFolder = Instance.new("Folder")
  119.     teamKillsFolder.Name = "TEAM KILLS"
  120.     teamKillsFolder.Parent = rs
  121.    
  122.     local team1Kills = Instance.new("IntValue")
  123.     team1Kills.Name = "TEAM 1"
  124.     team1Kills.Parent = teamKillsFolder
  125.    
  126.     local team2Kills = Instance.new("IntValue")
  127.     team2Kills.Name = "TEAM 2"
  128.     team2Kills.Parent = teamKillsFolder
  129.    
  130.    
  131.     for i, plr in pairs(game.Players:GetPlayers()) do
  132.  
  133.         local char = plr.Character
  134.         if char and char.Humanoid.Health > 0 then
  135.  
  136.             if i % 2 == 1 then
  137.                 plr.Team = team1
  138.                 char.HumanoidRootPart.CFrame = mapChosen.Spawns.Team1Spawn.CFrame
  139.                
  140.                 for i, weapon in pairs(weapons.Team1Weapons:GetChildren()) do
  141.                     weapon:Clone().Parent = plr.Backpack
  142.                 end
  143.  
  144.             else
  145.                 plr.Team = team2
  146.                 char.HumanoidRootPart.CFrame = mapChosen.Spawns.Team2Spawn.CFrame
  147.                
  148.                 for i, weapon in pairs(weapons.Team2Weapons:GetChildren()) do
  149.                     weapon:Clone().Parent = plr.Backpack
  150.                 end
  151.             end
  152.            
  153.            
  154.             local connection1 = char.Humanoid.Died:Connect(function()
  155.                 if plr.Team == team1 then
  156.                     team2Kills.Value += 1
  157.                 elseif plr.Team == team2 then
  158.                     team1Kills.Value += 1
  159.                 end
  160.             end)
  161.            
  162.             local connection3 = nil
  163.             local connection2 = plr.CharacterAdded:Connect(function(newChar)
  164.                 local hum = newChar:WaitForChild("Humanoid")
  165.                
  166.                 if plr.Team == team1 then
  167.                     for i, weapon in pairs(weapons.Team1Weapons:GetChildren()) do
  168.                         weapon:Clone().Parent = plr.Backpack
  169.                     end
  170.                     newChar.HumanoidRootPart:GetPropertyChangedSignal("CFrame"):Wait()
  171.                     newChar.HumanoidRootPart.CFrame = mapChosen.Spawns.Team1Spawn.CFrame
  172.                    
  173.                 elseif plr.Team == team2 then
  174.                     for i, weapon in pairs(weapons.Team2Weapons:GetChildren()) do
  175.                         weapon:Clone().Parent = plr.Backpack
  176.                     end
  177.                     newChar.HumanoidRootPart:GetPropertyChangedSignal("CFrame"):Wait()
  178.                     newChar.HumanoidRootPart.CFrame = mapChosen.Spawns.Team2Spawn.CFrame
  179.                 end
  180.                
  181.                 connection3 = hum.Died:Connect(function()
  182.                     if plr.Team == team1 then
  183.                         team2Kills.Value += 1
  184.                     elseif plr.Team == team2 then
  185.                         team1Kills.Value += 1
  186.                     end
  187.                 end)
  188.             end)
  189.  
  190.             teamKillsFolder.Destroying:Connect(function()
  191.                 connection1:Disconnect()
  192.                 connection2:Disconnect()
  193.                 connection3:Disconnect()
  194.             end)
  195.         end
  196.     end
  197.    
  198.    
  199.     local gameStart = tick()
  200.    
  201.     while true do
  202.        
  203.         local now = tick()
  204.         local timeLeft = config.RoundTime - math.round(now - gameStart)
  205.        
  206.         local mins = math.floor(timeLeft / 60)
  207.         local secs = timeLeft - (mins * 60)
  208.         if string.len(secs) < 2 then
  209.             secs = "0" .. tostring(secs)
  210.         end
  211.  
  212.         statusValue.Value = mins .. ":" .. secs
  213.        
  214.        
  215.         if timeLeft <= 0 or #team1:GetPlayers() < 1 or #team2:GetPlayers() < 1 then
  216.            
  217.             if team1Kills.Value > team2Kills.Value then
  218.                 rewardWinner(team1)
  219.             elseif team2Kills.Value > team1Kills.Value then
  220.                 rewardWinner(team2)
  221.             else
  222.                 statusValue.Value = "Draw!"
  223.             end
  224.             break
  225.        
  226.         elseif config.StopOnceKillsReached then
  227.            
  228.             if team1Kills.Value >= config.KillsRequiredToStop then
  229.                 rewardWinner(team1)
  230.                 break
  231.                
  232.             elseif team2Kills.Value >= config.KillsRequiredToStop then
  233.                 rewardWinner(team2)
  234.                 break
  235.             end
  236.         end
  237.        
  238.         game:GetService("RunService").Heartbeat:Wait()
  239.     end
  240.    
  241.    
  242.     for i, plr in pairs(game.Players:GetPlayers()) do
  243.         plr.Team = nil
  244.         plr:LoadCharacter()
  245.     end
  246.    
  247.     mapChosen:Destroy()
  248.     teamKillsFolder:Destroy()
  249.    
  250.     task.wait(config.RoundFinishedTime)
  251. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement