Advertisement
sriyanto

RoundModule (Piggy Game)

Apr 6th, 2024 (edited)
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. local module = {}
  2. local status = game.ReplicatedStorage:WaitForChild("Status")
  3. function module.Intermission(length)
  4.     for i = length,0,-1 do
  5.         status.Value = "Next round starts in "..i.." seconds"
  6.         wait(1)
  7.     end
  8. end
  9. function module.SelectChapter()
  10.     local Votes = {}
  11.     game.ReplicatedStorage.ToggleMapVote:FireAllClients(true)
  12.     for i, v in pairs(game.ReplicatedStorage.Chapters:GetChildren()) do
  13.         Votes[v.Name] = {}
  14.     end
  15.     local placeVoteConnection = game.ReplicatedStorage.PlaceVote.OnServerEvent:Connect(function(player,mapName)
  16.         if Votes[mapName] then
  17.             for i, playerVotesTab in pairs(Votes) do
  18.                 for x, playerName in pairs(playerVotesTab) do
  19.                     if playerName == player.Name then
  20.                         table.remove(playerVotesTab,x)
  21.                         break
  22.                     end
  23.                 end
  24.             end    
  25.             table.insert(Votes[mapName],player.Name)       
  26.             game.ReplicatedStorage.UpdateVoteCount:FireAllClients(Votes)       
  27.         end
  28.     end)
  29.     game.ReplicatedStorage.MapVoting.Value = true
  30.     wait(15)
  31.     game.ReplicatedStorage.MapVoting.Value = false
  32.     game.ReplicatedStorage.ToggleMapVote:FireAllClients(false)
  33.     placeVoteConnection:Disconnect()
  34.     local mostVotes = nil
  35.     local mostVoted = nil
  36.     for i, mapTable in pairs(Votes) do 
  37.         local votes = #mapTable
  38.         if mostVotes == nil then
  39.             mostVotes = votes
  40.             mostVoted = i  
  41.         else       
  42.             if votes >= mostVotes then
  43.                 mostVotes = votes
  44.                 mostVoted = i
  45.             end
  46.         end
  47.     end
  48.     local chosenChapter
  49.     if mostVotes == nil or mostVoted == nil then
  50.         chosenChapter = game.ReplicatedStorage.Chapters:GetChildren()[math.random(1,#game.ReplicatedStorage.Chapters:GetChildren())]
  51.     else
  52.         chosenChapter = game.ReplicatedStorage.Chapters[mostVoted]
  53.     end
  54.     status.Value = chosenChapter.Name.." has been selected with "..tostring(mostVotes)
  55.     wait(5)
  56.     return chosenChapter
  57. end
  58. function module.ChoosePiggy(players)
  59.     local RandomObj = Random.new()
  60.     local chosenPiggy = players[RandomObj:NextInteger(1,#players)]
  61.     return chosenPiggy
  62. end
  63. function module.DressPiggy(piggy)
  64.     local character
  65.     if piggy.EquippedSkin.Value ~= "" then
  66.         if game.ReplicatedStorage.Skins:FindFirstChild(piggy.EquippedSkin.Value) then
  67.             character = game.ReplicatedStorage.Skins[piggy.EquippedSkin.Value]:Clone()
  68.         end
  69.     else
  70.         character = game.ReplicatedStorage.Skins.Piggy:Clone()
  71.     end
  72.     character.Name = piggy.Name
  73.     piggy.Character = character
  74.     character.Parent = workspace
  75. end
  76. function module.TeleportPiggy(player)
  77.     if player.Character then   
  78.         player.Character.Humanoid.WalkSpeed = 14   
  79.         local bat = game.ServerStorage.Tools.PiggyBat:Clone()
  80.         bat.Parent = player.Character
  81.         if player.Character:FindFirstChild("HumanoidRootPart") then
  82.             player.Character.HumanoidRootPart.CFrame = game.Workspace.WaitingRoom.PiggyWaitingSpawn.CFrame + Vector3.new(0,5,0)
  83.         end
  84.         local TrapCount = Instance.new("IntValue")
  85.         TrapCount.Name = "TrapCount"
  86.         TrapCount.Value = 5
  87.         TrapCount.Parent = player  
  88.         game.ReplicatedStorage.ToggleTrap:FireClient(player,true)      
  89.     end
  90. end
  91. function module.TeleportPlayers(players, mapSpawns)
  92.     -- 'players' will be a Table containing all contestants' player objects
  93.     for i, player in pairs(players) do
  94.         if player.Character then
  95.             local character = player.Character
  96.             if character:FindFirstChild("HumanoidRootPart") then           
  97.                 player.Character.Humanoid.WalkSpeed = 16           
  98.                 local rand = Random.new()
  99.                 player.Character.HumanoidRootPart.CFrame = mapSpawns[rand:NextInteger(1,#mapSpawns)].CFrame + Vector3.new(0,10,0)      
  100.                 local hitboxClone = game.ServerStorage.Hitbox:Clone()
  101.                 hitboxClone.CFrame = character.HumanoidRootPart.CFrame     
  102.                 local weld = Instance.new("Weld")
  103.                 weld.Part1 = character.HumanoidRootPart
  104.                 weld.Part0 = hitboxClone
  105.                 weld.Parent = character    
  106.                 hitboxClone.Parent = player.Character                      
  107.             end
  108.         end
  109.     end
  110. end
  111. function module.InsertTag(contestants,tagName)
  112.     for i, player in pairs(contestants) do
  113.         local Tag = Instance.new("StringValue")
  114.         Tag.Name = tagName
  115.         Tag.Parent = player
  116.     end
  117. end
  118. local function toMS(s)
  119.     return ("%02i:%02i"):format(s/60%60, s%60) -- 600 --> 10:00
  120. end
  121. function module.StartRound(length,piggy,chapterMap) -- length (in seconds)
  122.     local outcome
  123.     game.ServerStorage.GameValues.GameInProgress.Value = true
  124.     for i = length,0,-1 do 
  125.         if i == (length - 20) then
  126.             module.TeleportPlayers({piggy},chapterMap.PlayerSpawns:GetChildren())
  127.             status.Value = "Piggy has woken up!"
  128.             wait(2)
  129.         end
  130.         local contestants = {} 
  131.         local isPiggyHere = false  
  132.         local Escapees = 0
  133.         for i, player in pairs(game.Players:GetPlayers()) do           
  134.             if player:FindFirstChild("Contestant") then
  135.                 table.insert(contestants,player)
  136.             elseif player:FindFirstChild("Piggy") then
  137.                 isPiggyHere = true
  138.             end    
  139.             if player:FindFirstChild("Escaped") then
  140.                 Escapees = Escapees + 1
  141.             end        
  142.         end    
  143.         status.Value = toMS(i)     
  144.         if Escapees > 0 then
  145.             outcome = "escaped"
  146.             for i, v in pairs(contestants) do
  147.                 if v.Tokens then
  148.                     v.Tokens.Value = v.Tokens.Value + 50
  149.                 end
  150.             end
  151.             break
  152.         end    
  153.         if not isPiggyHere then -- if isPiggyHere == false / is the piggy dead / left the game?
  154.             outcome = "piggy-left"
  155.             for i, v in pairs(contestants) do
  156.                 if v.Tokens then
  157.                     v.Tokens.Value = v.Tokens.Value + 50
  158.                 end
  159.             end
  160.             break
  161.         end    
  162.         if #contestants == 0 then
  163.             outcome = "piggy-killed-everyone"
  164.             break
  165.         end    
  166.         if i == 0 then
  167.             outcome = "time-up"
  168.             break
  169.         end    
  170.         wait(1)
  171.     end
  172.     if outcome == "piggy-killed-everyone" then
  173.         status.Value = "The Piggy Has Killed Everyone"
  174.     elseif outcome == "time-up" then
  175.         status.Value = "Time Up - Contestants Win"
  176.     elseif outcome == "piggy-left" then
  177.         status.Value = "Piggy has died / left! Contestants win!"
  178.     elseif outcome == "escaped" then
  179.         status.Value = "Contestants have escaped! Piggy loses!"
  180.     end
  181.     wait(5)
  182. end
  183. function module.RemoveTags()
  184.     game.ServerStorage.GameValues.GameInProgress.Value = false
  185.     game.ReplicatedStorage.ToggleCrouch:FireAllClients(false)
  186.     for i, v in pairs(game.Players:GetPlayers()) do
  187.         if v:FindFirstChild("Piggy") then
  188.             v.Piggy:Destroy()      
  189.             if v.Backpack:FindFirstChild("PiggyBat") then v.Backpack.PiggyBat:Destroy() end
  190.             if v.Character:FindFirstChild("PiggyBat") then v.Character.PiggyBat:Destroy() end      
  191.             if v:FindFirstChild("TrapCount") then
  192.                 v.TrapCount:Destroy()
  193.             end
  194.             game.ReplicatedStorage.ToggleTrap:FireClient(v,false)      
  195.             v:LoadCharacter()      
  196.         elseif v:FindFirstChild("Contestant") then
  197.             v.Contestant:Destroy()
  198.             for _, p in pairs(v.Backpack:GetChildren()) do
  199.                 if p:IsA("Tool") then
  200.                     p:Destroy()
  201.                 end
  202.             end    
  203.             for _, p in pairs(v.Character:GetChildren()) do
  204.                 if p:IsA("Tool") then
  205.                     p:Destroy()
  206.                 end
  207.             end    
  208.         end
  209.         if v:FindFirstChild("Escaped") then
  210.             v.Escaped:Destroy()
  211.         end
  212.     end
  213. end
  214. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement