Advertisement
sriyanto

Server Squid game

Apr 4th, 2024
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.43 KB | None | 0 0
  1. local TweenService = game:GetService("TweenService")
  2. local Players = game:GetService("Players")
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local Map = game.Workspace.Map
  5. local Doll = Map.Doll
  6. local TimerUI = Map.Timer.UI
  7. local ROUND_DURATION_SECONDS = 60 --1 minute
  8. local isRedLight = ReplicatedStorage.isRedLight
  9. local savedPlayerPositions = {} --logging all player positions when there is a red light, so we can later check their movements
  10. local SpinTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
  11. local OriginalHeadCFrame = Doll.Head.CFrame
  12. local GreenLightHead = TweenService:Create(Doll.Head, SpinTweenInfo, {
  13.     CFrame = OriginalHeadCFrame
  14. })
  15. local RedLightHead = TweenService:Create(Doll.Head, SpinTweenInfo, {
  16.     CFrame = OriginalHeadCFrame * CFrame.Angles(0,math.rad(180),0) --180 Degrees Rotation
  17. })
  18. function DressCharacterInSuit(character)
  19.     for _, object in pairs(character:GetChildren()) do
  20.         if object:IsA("Shirt") or object:IsA("Pants") or object:IsA("ShirtGraphic") then
  21.             object:Destroy()
  22.         end
  23.     end
  24.     local Shirt = Instance.new("Shirt")
  25.     Shirt.ShirtTemplate = "rbxassetid://7597521537"
  26.     Shirt.Parent = character
  27.     local Pants = Instance.new("Pants")
  28.     Pants.PantsTemplate = "rbxassetid://7597288954"
  29.     Pants.Parent = character
  30. end
  31. function SecondsToTimestamp(seconds)
  32.     seconds = math.max(seconds, 0)
  33.     local minutes = tostring(math.floor(seconds/60))
  34.     local leftoverSeconds = tostring(seconds%60)
  35.     if #leftoverSeconds ==1 then
  36.         leftoverSeconds = "0"..leftoverSeconds
  37.     end
  38.     return tostring(minutes)..":"..leftoverSeconds
  39. end
  40. function LogPlayerPositions(players)
  41.     for _, player in pairs(players) do
  42.         if player.Character then
  43.             local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
  44.             if HumanoidRootPart then
  45.                 savedPlayerPositions[player] = HumanoidRootPart.Position
  46.             end
  47.         end
  48.     end
  49. end
  50. function StartGame()
  51.     GreenLightHead:Play()
  52.     isRedLight.Value = false
  53.     savedPlayerPositions = {}
  54.     local playersInRound = Players:GetPlayers()
  55.     local winners = {}
  56.     local spinDelay = 3
  57.     local lastSpin = tick()
  58.     local endTouch
  59.     for _, player in pairs(playersInRound) do
  60.         player:LoadCharacter()
  61.         DressCharacterInSuit(player.Character)
  62.     end
  63.     endTouch = Map.EndBarrier.Touched:Connect(function(toucher)
  64.         if not toucher or not toucher.Parent then
  65.             return
  66.         end
  67.         local touchChar = toucher.Parent
  68.         local touchPlayer = Players:GetPlayerFromCharacter(touchChar)
  69.         if touchPlayer then
  70.             local winnersIndex = table.find(winners, touchPlayer)
  71.             if winnersIndex then return end
  72.             table.insert(winners, touchPlayer)
  73.             local playerIndex = table.find(playersInRound, touchPlayer)
  74.             if playerIndex then
  75.                 table.remove(playersInRound, playerIndex)
  76.             end
  77.         end
  78.     end)
  79.     --keeping the timer updated
  80.     --checking if players move
  81.     for i =ROUND_DURATION_SECONDS, 0, -1 do
  82.         TimerUI.TimeLabel.Text = SecondsToTimestamp(i)
  83.         if isRedLight.Value then
  84.             --Get the position of players, check if players have moved
  85.             for player, savedPosition in pairs(savedPlayerPositions) do
  86.                 if player.Character then
  87.                     local winnerIndex = table.find(winners,player)
  88.                     if winnerIndex then
  89.                         continue
  90.                     end
  91.                     local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
  92.                     if HumanoidRootPart then
  93.                         if(HumanoidRootPart.Position-savedPosition).Magnitude >= 1 then
  94.                             player.Character:BreakJoints() --kill the player
  95.                             savedPlayerPositions[player] = nil
  96.                             --Remove the player from the round table
  97.                             local playerIndex = table.find(playersInRound, player)
  98.                             if playerIndex then
  99.                                 table.remove(playersInRound, playerIndex)
  100.                             end
  101.                         end
  102.                     end
  103.                 end
  104.             end
  105.         end
  106.         if tick()-lastSpin >= spinDelay then --Has 5 seconds gone by since the last turn
  107.             if isRedLight.Value == true then
  108.                 --Make it a green light
  109.                 isRedLight.Value = false
  110.                 GreenLightHead:Play()
  111.             else
  112.                 --Make it a red light
  113.                 isRedLight.Value = true
  114.                 RedLightHead:Play()
  115.                 RedLightHead.Completed:Wait()
  116.                 LogPlayerPositions(playersInRound)
  117.             end
  118.             lastSpin = tick()
  119.             spinDelay = spinDelay * 0.95 -- Make it 10% faster each time
  120.         end
  121.     if #playersInRound == 0 then
  122.             break
  123.         end
  124.         task.wait(1) --Important
  125.     end
  126.     --by this time, the game is over
  127.     endTouch:Disconnect()
  128.     for _, winner in pairs(winners) do
  129.         winner.leaderstats.Cash.Value +=5
  130.         if winner.Character then
  131.         end
  132.     end
  133.     print("Round Over!")
  134. end
  135. --Main game loop
  136. while true do
  137.     print("Waiting 5 seconds before starting a new game.")
  138.     task.wait(5)
  139.     StartGame()
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement