HowToRoblox

GameHandler

Mar 31st, 2021 (edited)
1,606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. local infectedSpawn = script.Parent.Spawns:WaitForChild("InfectedSpawn")
  2.  
  3.  
  4. local roundTime = 30
  5.  
  6. local statusValue = game.ReplicatedStorage:WaitForChild("GameStatus")
  7.  
  8.  
  9. game.Players.PlayerAdded:Connect(function(plrJoined)
  10.    
  11.     local ls = Instance.new("Folder")
  12.     ls.Name = "leaderstats"
  13.     ls.Parent = plrJoined
  14.    
  15.     local cash = Instance.new("IntValue")
  16.     cash.Name = "Cash"
  17.     cash.Parent = ls
  18. end)
  19.  
  20.  
  21. local gameActive = false
  22. local survivors = {}
  23.  
  24.  
  25. function handleInfected(plr)
  26.    
  27.     local c = plr.Character
  28.    
  29.     c.HumanoidRootPart.CFrame = infectedSpawn.CFrame + Vector3.new(0, 10, 0)
  30.  
  31.     c["Body Colors"].HeadColor3, c["Body Colors"].LeftArmColor3, c["Body Colors"].RightArmColor3, c["Body Colors"].TorsoColor3, c["Body Colors"].RightLegColor3, c["Body Colors"].LeftLegColor3 = Color3.fromRGB(21, 255, 75),Color3.fromRGB(21, 255, 75),Color3.fromRGB(21, 255, 75),Color3.fromRGB(21, 255, 75),Color3.fromRGB(21, 255, 75),Color3.fromRGB(21, 255, 75)
  32.  
  33.  
  34.     c.Humanoid.Touched:Connect(function(part)
  35.  
  36.         local hitPlr = game.Players:GetPlayerFromCharacter(part.Parent)
  37.  
  38.         if hitPlr and table.find(survivors, hitPlr) and gameActive then
  39.  
  40.             table.remove(survivors, table.find(survivors, hitPlr))
  41.  
  42.             hitPlr:LoadCharacter()
  43.             hitPlr.Character.HumanoidRootPart.CFrame = infectedSpawn.CFrame + Vector3.new(0, 10, 0)
  44.            
  45.            
  46.             handleInfected(hitPlr)
  47.         end
  48.     end)
  49.    
  50.    
  51.     c.Humanoid.Died:Connect(function()
  52.  
  53.         plr:LoadCharacter()
  54.         handleInfected(plr)
  55.     end)
  56. end
  57.  
  58.  
  59.  
  60. while true do
  61.    
  62.     statusValue.Value = ""
  63.    
  64.     repeat wait() until #game.Players:GetPlayers() > 1
  65.     local plrs = game.Players:GetPlayers()
  66.    
  67.    
  68.     local spawns = script.Parent:WaitForChild("Spawns"):GetChildren()
  69.     table.remove(spawns, table.find(spawns, infectedSpawn))
  70.    
  71.    
  72.     local infected = plrs[math.random(1, #plrs)]
  73.    
  74.     for i, plr in pairs(plrs) do
  75.        
  76.         plr:LoadCharacter()
  77.         local c = plr.Character
  78.        
  79.         if plr == infected then        
  80.            
  81.             handleInfected(plr)
  82.            
  83.         else
  84.            
  85.             local chosenSpawn = spawns[math.random(1, #spawns)]
  86.             table.remove(spawns, table.find(spawns, chosenSpawn))
  87.            
  88.             c.HumanoidRootPart.CFrame = chosenSpawn.CFrame + Vector3.new(0, 10, 0)
  89.            
  90.             table.insert(survivors, plr)
  91.            
  92.             c.Humanoid.Died:Connect(function()
  93.                
  94.                 plr:LoadCharacter()
  95.                 handleInfected(plr)
  96.             end)
  97.         end
  98.     end
  99.    
  100.    
  101.     gameActive = true
  102.     for i = roundTime, 0, -1 do
  103.        
  104.         wait(1)
  105.        
  106.         if #survivors < 1 or not infected then break end
  107.        
  108.        
  109.         statusValue.Value = i .. "s | " .. #survivors .. " survivors"
  110.     end
  111.    
  112.    
  113.     local winMsg = ""
  114.    
  115.     if #survivors < 1 then
  116.        
  117.         infected.leaderstats.Cash.Value += 100
  118.         winMsg = "Infected wins!"
  119.        
  120.     else
  121.        
  122.         for i, survivor in pairs(survivors) do
  123.            
  124.             survivor.leaderstats.Cash.Value += 20
  125.         end
  126.         winMsg = "Survivors win!"
  127.     end
  128.    
  129.     statusValue.Value = winMsg
  130.     gameActive = false
  131.     survivors = {}
  132.     wait(5)
  133. end
Add Comment
Please, Sign In to add comment