Advertisement
Cat_in_the_hat

Vote map versión 1

Jul 15th, 2024 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.88 KB | None | 0 0
  1. local alreadyVoted = {}
  2. local caveVotes = 0
  3. local desertVotes = 0
  4. local landVotes = 0
  5. local reset = false
  6.  
  7. local countdownLabels = {}
  8. local voteLabels = {}
  9. local promptList = {}
  10. local entityPositions = {}
  11. local timersEnded = false
  12. local timer = 30  -- the timer number like how long is the timer
  13.  
  14. local timerEndCount = 0
  15.  
  16. local players = PlayerService.getPlayers()
  17. local playerCount = #players
  18. local maxVotes = playerCount > 1 and math.ceil(playerCount / 2) or 1
  19. local voteCount = 0
  20.  
  21.  
  22. local function endTimers()
  23.     timersEnded = true
  24.     for _, label in pairs(countdownLabels) do
  25.         if label then
  26.             label:destroy()
  27.         end
  28.     end
  29. end
  30.  
  31. local function determineWinner(winner)
  32.     endTimers()
  33.     reset = true
  34.     local winningPosition
  35.     if winner == "cave" then
  36.         winningPosition = Vector3.new(207, 63, 243)
  37.     elseif winner == "desert" then
  38.         winningPosition = Vector3.new(222, 63, 243)
  39.     elseif winner == "land" then
  40.         winningPosition = Vector3.new(237, 63, 243)
  41.     end
  42.  
  43.     for _, player in ipairs(PlayerService.getPlayers()) do
  44.         local entity = player:getEntity()
  45.         if entity then
  46.             entity:setPosition(winningPosition)
  47.         end
  48.     end
  49.  
  50.     for _, position in ipairs(entityPositions) do
  51.         local nearbyEntities = EntityService.getNearbyEntities(position, 4)
  52.         if nearbyEntities then
  53.             for _, entity in ipairs(nearbyEntities) do
  54.                 local maxHealth = entity:getMaxHealth()
  55.                 if maxHealth and maxHealth > 0 then
  56.                     CombatService.damage(entity, maxHealth * 2)
  57.                 end
  58.             end
  59.         end
  60.     end
  61.  
  62.     for option, label in pairs(countdownLabels) do
  63.         if label then
  64.             label:destroy()
  65.         end
  66.     end
  67.  
  68.     for option, label in pairs(voteLabels) do
  69.         if label then
  70.             label:destroy()
  71.         end
  72.     end
  73.  
  74.     for _, player in ipairs(PlayerService.getPlayers()) do
  75.         local entity = player:getEntity()
  76.         if entity then
  77.             for _, option in ipairs({"cave", "desert", "land"}) do
  78.                 local imageEntity = EntityService.spawnImageEntity("rbxassetid://image_id_here", entity:getPosition() + Vector3.new(0, 18, 0))
  79.                 if imageEntity then
  80.                     imageEntity:setCustomName(option)
  81.                     imageEntity:setMaxHealth(-99999999999999999999999999999)
  82.                 end
  83.             end
  84.         end
  85.     end
  86.  
  87.     for option, prompt in pairs(promptList) do
  88.         if prompt then
  89.             prompt:destroy()
  90.         end
  91.     end
  92.  
  93.     caveVotes = 0
  94.     desertVotes = 0
  95.     landVotes = 0
  96.     voteCount = 0
  97.     timersEnded = true
  98.     alreadyVoted = {}
  99.     entityPositions = {}
  100. end
  101.  
  102. local function checkForWinner()
  103.     local winner
  104.     local highestVotes = math.max(caveVotes, desertVotes, landVotes)
  105.     local tiedOptions = {}
  106.  
  107.     if caveVotes == highestVotes then
  108.         table.insert(tiedOptions, "cave")
  109.     end
  110.     if desertVotes == highestVotes then
  111.         table.insert(tiedOptions, "desert")
  112.     end
  113.     if landVotes == highestVotes then
  114.         table.insert(tiedOptions, "land")
  115.     end
  116.  
  117.     if #tiedOptions > 1 then
  118.         winner = tiedOptions[math.random(#tiedOptions)]
  119.     else
  120.         winner = tiedOptions[1]
  121.     end
  122.  
  123.     determineWinner(winner)
  124. end
  125.  
  126. local function createVoteUI(votePosition, option, mapId)
  127.     local entity = EntityService.spawnImageEntity("rbxassetid://" .. mapId, votePosition + Vector3.new(0, 18, 0))
  128.     if not entity then return end
  129.  
  130.     entity:setCustomName(option)
  131.     entity:setMaxHealth(999999)
  132.     local b = entity:getModel()
  133.     b:setScale(3)
  134.     b:setCollidable(false)
  135.     b:setAnchored(true)
  136.     table.insert(entityPositions, votePosition + Vector3.new(0, 18, 0))
  137.  
  138.     Events.EntityDamage(function(event)
  139.         if event.entity == entity then
  140.             event.knockback.horizontal = 0
  141.             event.knockback.vertical = 0
  142.         end
  143.     end)
  144.  
  145.     local voteTextPosition = votePosition + Vector3.new(0, 8.5, 0)
  146.     voteLabels[option] = UIService.createTextLabel("0/" .. tostring(maxVotes), voteTextPosition)
  147.     voteLabels[option]:setBackgroundColor(Color3.fromRGB(0, 0, 0))
  148.     voteLabels[option]:setBackgroundTransparency(0.9)
  149.     voteLabels[option]:setTextColor(Color3.fromRGB(255, 255, 255))
  150.     voteLabels[option]:setSize(UDim2.fromScale(10, 2))
  151.     voteLabels[option]:setFont(Font.Arcade)
  152.  
  153.     local countdownPosition = votePosition + Vector3.new(0, 10, 0)
  154.     countdownLabels[option] = UIService.createTextLabel("⏱️ " .. timer, countdownPosition)
  155.  
  156.     task.spawn(function()
  157.         local timeLeft = timer
  158.         while timeLeft > 0 and not timersEnded and voteCount < maxVotes do
  159.             wait(1)
  160.             timeLeft = timeLeft - 1
  161.             countdownLabels[option]:setText("⏱️ " .. timeLeft)
  162.             SoundService.playSound(SoundType.COUNTDOWN_TICK, votePosition)
  163.         end
  164.         if not timersEnded then
  165.             countdownLabels[option]:destroy()
  166.             timerEndCount = timerEndCount + 1
  167.             if timerEndCount == 3 then
  168.                 checkForWinner()
  169.             end
  170.         end
  171.     end)
  172.  
  173.     local promptPosition = votePosition + Vector3.new(0, 3, 0)
  174.     local prompt = PromptService.createPrompt()
  175.     prompt:setPosition(promptPosition)
  176.     prompt:setActivationDistance(5)
  177.     prompt:setHoldDuration(0.5)
  178.     prompt:setObjectText(option)
  179.     prompt:setActionText("Vote")
  180.     promptList[option] = prompt
  181.  
  182.     prompt:onActivated(function(player)
  183.         if alreadyVoted[player.userId] then
  184.             MessageService.sendError(player, "Remove " .. option .. " vote.")
  185.             local previousVote = alreadyVoted[player.userId]
  186.             voteCount = voteCount - 1
  187.             voteLabels[previousVote]:setText(tostring(voteCount) .. "/" .. tostring(maxVotes))
  188.             alreadyVoted[player.userId] = nil
  189.             if previousVote == "cave" then
  190.                 caveVotes = caveVotes - 1
  191.             elseif previousVote == "desert" then
  192.                 desertVotes = desertVotes - 1
  193.             elseif previousVote == "land" then
  194.                 landVotes = landVotes - 1
  195.             end
  196.         else
  197.  
  198.             if voteCount < maxVotes and not timersEnded then
  199.                 voteCount = voteCount + 1
  200.                 voteLabels[option]:setText(tostring(voteCount) .. "/" .. tostring(maxVotes))
  201.                 alreadyVoted[player.userId] = option
  202.                 MessageService.sendInfo(player, "You voted for " .. option .. ".")
  203.  
  204.                 if option == "cave" then
  205.                     caveVotes = caveVotes + 1
  206.                 elseif option == "desert" then
  207.                     desertVotes = desertVotes + 1
  208.                 elseif option == "land" then
  209.                     landVotes = landVotes + 1
  210.                 end
  211.                 if caveVotes == maxVotes or desertVotes == maxVotes or landVotes == maxVotes then
  212.                     checkForWinner()
  213.                 end
  214.             end
  215.         end
  216.     end)
  217. end
  218.  
  219. local function createResetPrompt(resetPosition)
  220.     local prompt = PromptService.createPrompt()
  221.     prompt:setPosition(resetPosition)
  222.     prompt:setActivationDistance(5)
  223.     prompt:setHoldDuration(0)
  224.     prompt:setObjectText("Reset")
  225.     prompt:setActionText("Reset Voting")
  226.     prompt:onActivated(function(player)
  227.         if reset == false then
  228.             MessageService.sendError(player, "Wait till voting ends ")
  229.         else
  230.             timersEnded = false
  231.             voteCount = 0
  232.             alreadyVoted = {}
  233.             for _, label in pairs(countdownLabels) do
  234.                 if label then
  235.                     label:destroy()
  236.                 end
  237.             end
  238.             for _, prompt in pairs(promptList) do
  239.                 if prompt then
  240.                     prompt:destroy()
  241.                 end
  242.             end
  243.             countdownLabels = {}
  244.             voteLabels = {}
  245.             promptList = {}
  246.             createVoteUI(Vector3.new(207, 63, 216), "cave", "11467634330")
  247.             createVoteUI(Vector3.new(222, 63, 216), "desert", "11467634330")
  248.             createVoteUI(Vector3.new(237, 63, 216), "land", "11467634330")
  249.             reset = false
  250.             timerEndCount = 0
  251.         end
  252.     end)
  253. end
  254.  
  255. local vote1Position = Vector3.new(207, 63, 216)
  256. local vote2Position = Vector3.new(222, 63, 216)
  257. local vote3Position = Vector3.new(237, 63, 216)
  258.  -- where the prompts will spwwn at
  259.  
  260. local vote1MapId = "11467634330"
  261. local vote2MapId = "11467634330"
  262. local vote3MapId = "11467634330"
  263.  -- your maps image ids here
  264.  
  265. createVoteUI(vote1Position, "cave", vote1MapId)
  266. createVoteUI(vote2Position, "desert", vote2MapId)
  267. createVoteUI(vote3Position, "land", vote3MapId) -- map names
  268. createResetPrompt(Vector3.new(219, 66, 234))
  269.  
  270.  
  271.  
  272. task.spawn(function()
  273.     wait(timer + 1)
  274.     if not timersEnded then
  275.         checkForWinner()
  276.     end
  277. end)
  278.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement