Изменения и улучшения:
- Инициализация votes: Теперь мы инициализируем votes в начале функции ToggleVoting, чтобы избежать накопления старых голосов между раундами.
- Обработка голосов: Убедитесь, что вы правильно собираете голоса от игроков. Вам нужно добавить логику для сбора голосов за карты в ToggleVoting.
- Проверка существования карты: В функции LoadMap добавлена проверка на существование карты "GrassLand".
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local info = workspace.Info
local round = {}
local votes = {}
local mob = require(script.Parent.Mob)
function round.LoadMap()
local votedMap = round.ToggleVoting()
print("Voted map:", votedMap) -- Выводим имя выбранной карты
if not ServerStorage.Maps then
error("Maps folder does not exist in ServerStorage")
end
local mapFolder = ServerStorage.Maps:FindFirstChild(votedMap)
if not mapFolder then
print("Map not found, using default GrassLand.")
mapFolder = ServerStorage.Maps:FindFirstChild("GrassLand")
end
local newMap = mapFolder:Clone()
newMap.Parent = workspace.Map
workspace.SpawnBox.Floor:Destroy()
newMap.Base.Humanoid.HealthChanged:Connect(function(health)
if health <= 0 then
info.GameRunning.Value = false
info.Message.Value = "GAME OVER"
end
end)
return newMap
end
function round.ToggleVoting()
for i = 10, 1, -1 do
info.Message.Value = "Map voting (" .. i .. ")"
print("Map voting (" .. i .. ")")
task.wait(1)
end
local maps = ServerStorage.Maps:GetChildren()
votes = {} -- Инициализируем таблицу голосов
for i, map in ipairs(maps) do
votes[map.Name] = {}
end
-- Здесь вы должны добавить логику для сбора голосов от игроков
local winVote = nil
local winScore = 0
for name, map in pairs(votes) do
if #map > winScore then
winScore = #map
winVote = name
end
end
if not winVote then
local n = math.random(#maps)
winVote = maps[n].Name
end
return winVote -- Возвращаем имя карты
end
function round.ProcessVote(player, vote)
if votes[vote] == nil then
votes[vote] = {}
end
-- Удаляем старый голос, если он есть
for name, mapVotes in pairs(votes) do
local oldVoteIndex = table.find(mapVotes, player.UserId)
if oldVoteIndex then
table.remove(mapVotes, oldVoteIndex)
print("Old vote found, removed:", oldVoteIndex)
break
end
end
-- Добавляем новый голос
print("Processed vote for", vote)
table.insert(votes[vote], player.UserId)
events:WaitForChild("UpdateVoteCount"):FireAllClients()
end
events:WaitForChild("VoteForMap").OnServerEvent:Connect(round.ProcessVote)
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local info = workspace.Info
local round = {}
local votes = {}
local mob = require(script.Parent.Mob)
function round.LoadMap()
local votedMap = round.ToggleVoting()
print("Voted map:", votedMap) -- Выводим имя выбранной карты
if not ServerStorage.Maps then
error("Maps folder does not exist in ServerStorage")
end
local mapFolder = ServerStorage.Maps:FindFirstChild(votedMap)
if not mapFolder then
print("Map not found, using default GrassLand.")
mapFolder = ServerStorage.Maps.GrassLand
end
local newMap = mapFolder:Clone()
newMap.Parent = workspace.Map
workspace.SpawnBox.Floor:Destroy()
newMap.Base.Humanoid.HealthChanged:Connect(function(health)
if health <= 0 then
info.GameRunning.Value = false
info.Message.Value = "GAME OVER"
end
end)
return newMap
end
function round.ToggleVoting()
for i = 10, 1, -1 do
info.Message.Value = "Map voting (" .. i .. ")"
print("Map voting (" .. i .. ")")
task.wait(1)
end
local maps = ServerStorage.Maps:GetChildren()
for i, map in ipairs(maps) do
votes[ map.Name ] = {}
end
local winVote = nil
local winScore = 0
for name, map in pairs(votes) do
if #map > winScore then
winScore = #map
winVote = name
end
end
if not winVote then
local n = math.random(#maps)
winVote = maps[n].Name
end
return winVote -- Возвращаем имя карты
end
function round.ProcessVote(player, vote)
for name, mapVotes in pairs(votes) do
local oldVote = table.find(mapVotes, player.UserId)
if oldVote then
table.remove(mapVotes, oldVote)
print("Old vote found", oldVote, "")
break
end
end
print("Processed vote for", vote)
if votes[vote] == nil then
votes[vote] = {}
end
table.insert(votes[vote], player.UserId)
events:WaitForChild("UpdateVoteCount"):FireAllClients()
end
events:WaitForChild("VoteForMap").OnServerEvent:Connect(round.ProcessVote)