Advertisement
HowToRoblox

MinigameServer

Feb 16th, 2023
1,447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.89 KB | None | 0 0
  1. --DATA HANDLING
  2. local dss = game:GetService("DataStoreService")
  3. local ds = dss:GetDataStore("DATA")
  4.  
  5.  
  6. function saveData(plr:Player)
  7.    
  8.     if not plr:FindFirstChild("DATA FAILED TO LOAD") then
  9.        
  10.         local plrCash = plr.leaderstats.Cash.Value
  11.        
  12.         local success, err = nil, nil
  13.         while not success do
  14.             success, err = pcall(function()
  15.                 ds:SetAsync(plr.UserId, plrCash)
  16.             end)
  17.             if err then
  18.                 warn(err)
  19.             end
  20.             task.wait(0.02)
  21.         end
  22.     end
  23. end
  24.  
  25.  
  26. game.Players.PlayerRemoving:Connect(saveData)
  27. game:BindToClose(function()
  28.     for _, plr in pairs(game.Players:GetPlayers()) do
  29.         saveData(plr)
  30.     end
  31. end)
  32.  
  33. game.Players.PlayerAdded:Connect(function(plr)
  34.  
  35.     local dataFailedWarning = Instance.new("StringValue")
  36.     dataFailedWarning.Name = "DATA FAILED TO LOAD"
  37.     dataFailedWarning.Parent = plr
  38.  
  39.     local success, plrCash = nil, nil
  40.     while not success do
  41.         success, plrCash = pcall(function()
  42.             return ds:GetAsync(plr.UserId)
  43.         end)
  44.         task.wait(0.02)
  45.     end
  46.     dataFailedWarning:Destroy()
  47.  
  48.     if not plrCash then
  49.         plrCash = 0
  50.     end
  51.  
  52.     local ls = Instance.new("Folder")
  53.     ls.Name = "leaderstats"
  54.  
  55.     local cashVal = Instance.new("IntValue")
  56.     cashVal.Name = "Cash"
  57.     cashVal.Value = plrCash
  58.     cashVal.Parent = ls
  59.  
  60.     ls.Parent = plr
  61. end)
  62.  
  63.  
  64. --GAME LOOP
  65. local minigameModules = game:GetService("ServerStorage"):WaitForChild("MinigameModules")
  66.  
  67. local minPlayersToStart = 2
  68. local intermissionTime = 5
  69. local timeToTeleportPlayers = 3
  70. local timeAfterEnd = 5
  71.  
  72. _G.rnd = Random.new()
  73.  
  74. local statusVal = Instance.new("StringValue")
  75. statusVal.Name = "GAME STATUS"
  76. statusVal.Parent = game:GetService("ReplicatedStorage")
  77.  
  78.  
  79. function getPlayerList()
  80.     local plrs = {}
  81.     for _, plr in pairs(game.Players:GetPlayers()) do
  82.         if (plr.Character and plr.Character:FindFirstChild("Humanoid") and plr.Character.Humanoid.Health > 0) then
  83.             table.insert(plrs, plr)
  84.         end
  85.     end
  86.     return plrs
  87. end
  88.  
  89.  
  90. while true do
  91.    
  92.     local plrsInGame = getPlayerList()
  93.     while #plrsInGame < minPlayersToStart do
  94.         statusVal.Value = "Waiting for " .. (minPlayersToStart - #plrsInGame) .. " more player" .. ((minPlayersToStart - #plrsInGame) ~= 1 and "s" or "") .. " to start"
  95.         task.wait(0.2)
  96.         plrsInGame = getPlayerList()
  97.     end
  98.    
  99.     for i = intermissionTime, 0, -1 do
  100.         statusVal.Value = "Game starting in " .. i .. "s"
  101.         task.wait(1)
  102.     end
  103.    
  104.     local allMinigames = minigameModules:GetChildren()
  105.     local chosenMinigame
  106.     while true do
  107.         chosenMinigame = require(allMinigames[_G.rnd:NextInteger(1, #allMinigames)])
  108.        
  109.         if (not chosenMinigame.Configuration["MINIMUM_PLAYERS"] or chosenMinigame.Configuration["MINIMUM_PLAYERS"] <= #plrsInGame) then
  110.             break
  111.         end
  112.     end
  113.    
  114.     statusVal.Value = "Now playing " .. chosenMinigame.Configuration["DISPLAY_NAME"]
  115.    
  116.     task.wait(timeToTeleportPlayers)
  117.    
  118.     local gameEnd = nil
  119.    
  120.     task.spawn(function()
  121.         local gameStart = tick()
  122.         while true do
  123.            
  124.             local timeSinceStart = tick() - gameStart
  125.             local timeLeft = math.clamp(chosenMinigame.Configuration["DURATION"] - timeSinceStart, 0, math.huge)
  126.            
  127.             local mins = tostring(math.floor(timeLeft / 60))
  128.             local secs = math.round(timeLeft - mins*60)
  129.             if string.len(tostring(secs)) < 2 then
  130.                 secs = "0" .. tostring(secs)
  131.             end
  132.            
  133.             if (not gameEnd and timeLeft > 0) then
  134.                 statusVal.Value = mins .. ":" .. secs
  135.             else
  136.                 break
  137.             end
  138.            
  139.             task.wait(0.2)
  140.         end
  141.     end)
  142.    
  143.     gameEnd = chosenMinigame.BeginGame(getPlayerList())
  144.    
  145.     local endMessage = "Game over!"
  146.     if #gameEnd > 0 then
  147.         endMessage = "Winners: "
  148.         for _, winner in pairs(gameEnd) do
  149.             endMessage = endMessage .. winner.Name
  150.            
  151.             if _ == #gameEnd - 1 then
  152.                 endMessage = endMessage .. " and "
  153.             elseif _ < #gameEnd then
  154.                 endMessage = endMessage .. ", "
  155.             end
  156.            
  157.             winner.leaderstats.Cash.Value += chosenMinigame.Configuration["REWARD"]
  158.            
  159.             winner:LoadCharacter()
  160.         end
  161.     end
  162.    
  163.     statusVal.Value = endMessage
  164.    
  165.     task.wait(timeAfterEnd)
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement