So im maded a map vote system and i got problem when the voting ends and map was chosen i think because its not returning winner name map but i dont know how to fix this
here is my 1 script(voting):
local module = {}
local status = game.ReplicatedStorage.Values.Status
local mapVotes = {}
function getMap()
local randomMap = game.ReplicatedStorage.Maps:GetChildren()[math.random(1,#game.ReplicatedStorage.Maps:GetChildren())]
-- if map exists in the mapVotes table
for i,map in pairs(mapVotes) do
if map.name == randomMap.Name then
return getMap()
end
end
return randomMap.Name
end
function module.MapVoting(dur)
-- Start the voting system
local event = game.ReplicatedStorage.Events.RemoteEvent
event.OnServerEvent:Connect(function()
mapVotes = {}
for i = 1,3 do
table.insert(mapVotes, {order = i; name = getMap(); players = {};})
end
game.ReplicatedStorage.Events.UpdateVotingSystem:FireAllClients(mapVotes)
game.ReplicatedStorage.Values.VotingProgress.Value = true
local placeVoteConnection = game.ReplicatedStorage.Events.PlaceVote.OnServerEvent:Connect(function(player,voteNumber)
-- Check if already voted
for i,map in pairs(mapVotes) do
for x,plr in pairs(map.players) do
if plr == player.UserId then
table.remove(map.players, x)
break
end
end
end
-- Place Vote
for i,map in pairs(mapVotes) do
if map.order == voteNumber then
table.insert(map.players, player.UserId)
end
end
game.ReplicatedStorage.Events.UpdateVotingSystem:FireAllClients(mapVotes)
end)
for i = dur,0,-1 do
if game.ReplicatedStorage.Values.VotingProgress.Value == true then
status.Value = "You have ".. i .." "..((i==1 and "second") or "seconds").." to vote!"
task.wait(1)
end
end
if game.ReplicatedStorage.Values.Status.Value == "You have 0 seconds to vote!" then
-- End the voting system
placeVoteConnection:Disconnect()
game.ReplicatedStorage.Values.VotingProgress.Value = false
-- Get the winner and reveal
table.sort(mapVotes, function(a,b) return #a.players > #b.players end)
local winner
-- If the all pads have the same vote
if #mapVotes[1].players == #mapVotes[2].players and #mapVotes[2].players == #mapVotes[3].players then
winner = mapVotes[math.random(1,3)]
-- If 2 pads have the same vote
elseif #mapVotes[1].players == #mapVotes[2].players then
winner = mapVotes[math.random(1,2)]
else
winner = mapVotes[1]
end
status.Value = winner.name.." was chosen!"
game.ReplicatedStorage.Values.value2.Value = 1
return game.ReplicatedStorage.Maps:FindFirstChild(winner.name)
end
end)
end
return module
here is my 2 script:
local voting = require(script.Voting)
while true do
local chosenMap = voting.MapVoting(20)
if game.ReplicatedStorage.Values.value2.Value == 1 then
local clonedMap = chosenMap:Clone()
clonedMap.Name = "Map"
clonedMap.Parent = workspace
end
wait(1)
end