Advertisement
HowToRoblox

VotingClient

Mar 21st, 2023
1,409
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1. local rs = game.ReplicatedStorage
  2.  
  3. local res = rs:WaitForChild("RemoteEvents")
  4.  
  5. local frame = script.Parent:WaitForChild("MapVoteFrame"); frame.Visible = false
  6. local templateFrame = script:WaitForChild("MapFrame")
  7.  
  8.  
  9. res:WaitForChild("VotingBegun").OnClientEvent:Connect(function(mapsToVote)
  10.    
  11.     for _, child in pairs(frame:WaitForChild("MapsContainer"):GetChildren()) do
  12.         if child.ClassName == templateFrame.ClassName then
  13.             child:Destroy()
  14.         end
  15.     end
  16.    
  17.     local mapFrames = {}
  18.    
  19.     for _, map in pairs(mapsToVote) do
  20.        
  21.         local newMapFrame = templateFrame:Clone()
  22.         newMapFrame.Name = map.Name
  23.         newMapFrame.MapName.Text = map.Name
  24.         newMapFrame.NumVotes.Text = "Votes: 0"
  25.         newMapFrame.MapImage.Image = "rbxassetid://" .. map.Configuration.ImageId.Value
  26.        
  27.         newMapFrame.VoteButton.MouseButton1Click:Connect(function()
  28.             res:WaitForChild("Voted"):FireServer(map.Name)
  29.         end)
  30.        
  31.         table.insert(mapFrames, newMapFrame)
  32.     end
  33.    
  34.     table.sort(mapFrames, function(a, b)
  35.         return a.Name < b.Name
  36.     end)
  37.    
  38.     for _, mapFrame in pairs(mapFrames) do
  39.         mapFrame.Parent = frame.MapsContainer
  40.     end
  41.    
  42.     frame.Visible = true
  43. end)
  44.  
  45.  
  46. res:WaitForChild("VotingEnded").OnClientEvent:Connect(function()
  47.    
  48.     frame.Visible = false
  49. end)
  50.  
  51.  
  52. res:WaitForChild("Voted").OnClientEvent:Connect(function(plrVotes)
  53.    
  54.     if frame.Visible == true then
  55.        
  56.         local votes = {}
  57.        
  58.         for plr, vote in pairs(plrVotes) do
  59.            
  60.             if not votes[vote] then
  61.                 votes[vote] = 1
  62.             else
  63.                 votes[vote] += 1
  64.             end
  65.         end
  66.        
  67.         for _, mapFrame in pairs(frame.MapsContainer:GetChildren()) do
  68.             if mapFrame.ClassName == templateFrame.ClassName then
  69.                 mapFrame.NumVotes.Text = "Votes: " .. (votes[mapFrame.Name] or 0)
  70.             end
  71.         end
  72.     end
  73. end)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement