Advertisement
HowToRoblox

GameHandler

Feb 7th, 2023 (edited)
1,698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.00 KB | None | 0 0
  1. local gameInProgress = false
  2.  
  3. local seekers = {}
  4. local hiders = {}
  5.  
  6. local transformingPlayers = {}
  7.  
  8.  
  9. --DATA HANDLING
  10. local dss = game:GetService("DataStoreService")
  11. local ds = dss:GetDataStore("CASH DATA")
  12.  
  13.  
  14. function saveData(plr)
  15.     if not plr:FindFirstChild("DATA FAILED TO LOAD") then
  16.  
  17.         local cash = plr.leaderstats.Cash.Value
  18.  
  19.         local success, err = nil, nil
  20.         while not success do
  21.             success, err = pcall(function()
  22.                 ds:SetAsync(plr.UserId, cash)
  23.             end)
  24.             if err then
  25.                 warn(err)
  26.             end
  27.             task.wait(0.02)
  28.         end
  29.     end
  30. end
  31.  
  32. game.Players.PlayerRemoving:Connect(saveData)
  33. game:BindToClose(function()
  34.     for _, plr in pairs(game.Players:GetPlayers()) do
  35.         saveData(plr)
  36.     end
  37. end)
  38.  
  39. game.Players.PlayerAdded:Connect(function(plr)
  40.    
  41.     plr.CharacterAdded:Connect(function(char)
  42.         if gameInProgress and not transformingPlayers[plr] then
  43.            
  44.             if table.find(hiders, plr) then
  45.                 table.remove(hiders, table.find(hiders, plr))
  46.             end
  47.            
  48.             char.HumanoidRootPart.CFrame = workspace.MAP.SPAWN.CFrame
  49.             for __, weapon in pairs(game.ReplicatedStorage:WaitForChild("SeekerWeapons"):GetChildren()) do
  50.                 weapon:Clone().Parent = plr.Backpack
  51.             end
  52.         end
  53.     end)
  54.  
  55.     local dataFailedWarning = Instance.new("StringValue")
  56.     dataFailedWarning.Name = "DATA FAILED TO LOAD"
  57.     dataFailedWarning.Parent = plr
  58.  
  59.     local success, plrData = nil, nil
  60.     while not success do
  61.         success, plrData = pcall(function()
  62.             return ds:GetAsync(plr.UserId)
  63.         end)
  64.         task.wait(0.02)
  65.     end
  66.     dataFailedWarning:Destroy()
  67.  
  68.     if not plrData then
  69.         plrData = 0
  70.     end
  71.  
  72.     local ls = Instance.new("Folder")
  73.     ls.Name = "leaderstats"
  74.  
  75.     local cashVal = Instance.new("IntValue")
  76.     cashVal.Name = "Cash"
  77.     cashVal.Value = plrData
  78.     cashVal.Parent = ls
  79.  
  80.     ls.Parent = plr
  81. end)
  82.  
  83.  
  84.  
  85. --TRANSFORMING INTO PROPS
  86. local maxDistanceFromProp = 10
  87.  
  88. game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("Transform").OnServerEvent:Connect(function(plr, object)
  89.    
  90.     if object and object:FindFirstChild("PROP") and gameInProgress and table.find(hiders, plr) then
  91.        
  92.         local char = plr.Character
  93.         if (char.HumanoidRootPart.Position - object:GetPivot().Position).Magnitude <= maxDistanceFromProp then
  94.            
  95.             transformingPlayers[plr] = true
  96.            
  97.             local newCharacter = game.ReplicatedStorage.PropCharacters[object.Name]:Clone()
  98.             newCharacter.Name = char.Name
  99.            
  100.             for _, child in pairs(char:GetChildren()) do
  101.                 if child:IsA("Script") or child:IsA("LocalScript") then
  102.                     child:Clone().Parent = newCharacter
  103.                 end
  104.             end
  105.            
  106.             newCharacter:PivotTo(char.HumanoidRootPart.CFrame)
  107.             plr.Character = newCharacter
  108.             newCharacter.Parent = workspace
  109.            
  110.             transformingPlayers[plr] = nil
  111.         end
  112.     end
  113. end)
  114.  
  115.  
  116. --MAIN GAME LOOP
  117. local minPlayersToStart = 2
  118.  
  119. local numSeekersStart = 1
  120.  
  121. local seekerReward = 40
  122. local hiderReward = 15
  123.  
  124.  
  125. local maps = game.ReplicatedStorage:WaitForChild("Maps")
  126.  
  127. local statusVal = Instance.new("StringValue")
  128. statusVal.Name = "GAME STATUS"
  129. statusVal.Parent = game.ReplicatedStorage
  130.  
  131.  
  132. local rnd = Random.new()
  133.  
  134.  
  135. function getPlayerList()
  136.    
  137.     local plrs = {}
  138.     for _, plr in pairs(game.Players:GetPlayers()) do
  139.         if plr.Character and plr.Character:FindFirstChild("Humanoid") and plr.Character.Humanoid.Health > 0 then
  140.             table.insert(plrs, plr)
  141.         end
  142.     end
  143.    
  144.     return plrs
  145. end
  146.  
  147.  
  148. while true do
  149.    
  150.     seekers = {}
  151.     hiders = {}
  152.    
  153.     local plrsInGame = getPlayerList()
  154.     while #plrsInGame < minPlayersToStart do
  155.         statusVal.Value = "Waiting for " .. (minPlayersToStart - #plrsInGame) .. " more player" .. ((minPlayersToStart - #plrsInGame) ~= 1 and "s" or "") .. " to start"
  156.         task.wait(0.2)
  157.         plrsInGame = getPlayerList()
  158.     end
  159.    
  160.     local intermissionTime = 5
  161.     for i = intermissionTime, 0, -1 do
  162.         statusVal.Value = "Choosing map in " .. i .. "s"
  163.         task.wait(1)
  164.     end
  165.    
  166.     plrsInGame = getPlayerList()
  167.    
  168.     if #plrsInGame >= minPlayersToStart then
  169.        
  170.         local chosenMap = maps:GetChildren()[rnd:NextInteger(1, #maps:GetChildren())]
  171.         local newMap = chosenMap:Clone()
  172.         newMap.Name = "MAP"
  173.         newMap.Parent = workspace
  174.  
  175.         statusVal.Value = "Map chosen: " .. chosenMap.Name
  176.  
  177.         for i = 1, numSeekersStart do
  178.             local randKey = rnd:NextInteger(1, #plrsInGame)
  179.             local randPlr = plrsInGame[randKey]
  180.             table.insert(seekers, randPlr)
  181.             table.remove(plrsInGame, randKey)
  182.         end
  183.         hiders = plrsInGame
  184.        
  185.         local timeBeforePlayersTeleported = 3
  186.         task.wait(timeBeforePlayersTeleported)
  187.        
  188.         gameInProgress = true
  189.        
  190.         local timeToHide = 10
  191.        
  192.         for _, hider in pairs(hiders) do
  193.             hider.Character.HumanoidRootPart.CFrame = newMap.SPAWN.CFrame
  194.         end
  195.        
  196.         for i = timeToHide, 0, -1 do
  197.             statusVal.Value = "Seekers will spawn in " .. i .. "s"
  198.             task.wait(1)
  199.         end
  200.        
  201.         for _, seeker in pairs(seekers) do
  202.             seeker.Character.HumanoidRootPart.CFrame = newMap.SPAWN.CFrame
  203.             for __, weapon in pairs(game.ReplicatedStorage:WaitForChild("SeekerWeapons"):GetChildren()) do
  204.                 weapon:Clone().Parent = seeker.Backpack
  205.             end
  206.         end
  207.        
  208.         local roundTime = 300
  209.         local roundStarted = tick()
  210.        
  211.         while true do
  212.             game:GetService("RunService").Heartbeat:Wait()
  213.            
  214.             local timeSinceStarted = tick() - roundStarted
  215.             if timeSinceStarted >= roundTime then break end
  216.            
  217.             local timeLeft = math.round(roundTime - timeSinceStarted)
  218.             statusVal.Value = "Round ends in " .. timeLeft .. "s"
  219.            
  220.             if #hiders == 0 then
  221.                 break
  222.             end
  223.         end
  224.        
  225.         gameInProgress = false
  226.        
  227.         if #hiders == 0 then
  228.             statusVal.Value = "Seekers win!"
  229.             for _, seeker in pairs(seekers) do
  230.                 seeker.leaderstats.Cash.Value += seekerReward
  231.             end
  232.         else
  233.             statusVal.Value = "Hiders win!"
  234.             for _, hider in pairs(hiders) do
  235.                 hider.leaderstats.Cash.Value += hiderReward
  236.             end
  237.         end
  238.  
  239.         for _, plr in pairs(game.Players:GetPlayers()) do
  240.             plr.Backpack:ClearAllChildren()
  241.             if plr.Character:FindFirstChildOfClass("Tool") then plr.Character:FindFirstChildOfClass("Tool"):Destroy() end
  242.             plr:LoadCharacter()
  243.         end
  244.        
  245.         newMap:Destroy()
  246.        
  247.         local timeAfterRound = 5
  248.         task.wait(timeAfterRound)
  249.     end
  250. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement