MapVote System problem

You can write your topic however you want, but you need to answer these questions:
i have this vote system + round system i want to let the players vote for a map and than add 1 to the map they choose

  1. *What is the issue?
    every round the player vote increase by 1 but i don’t want this to happen so let say the game in round 4 player vote will not add 1 it will add 4 to the choosen map
-- local script
local ChooseingMap = game.ReplicatedStorage:WaitForChild("chooseMaps")
local MineFrame = script.Parent.MineFrame
local updateVote = game.ReplicatedStorage:WaitForChild("updateVote")
local LocalPlayer =  game:GetService("Players").LocalPlayer
local AddRemoveVote = game.ReplicatedStorage:WaitForChild("AddRemoveVote")
local db = false
local PlayerVoteFor = 0


for Index,MapFrame in MineFrame:GetChildren() do
	local voteButton :TextButton = MapFrame.VoteButton
	voteButton.MouseButton1Click:Connect(function()
            --[[
                        
            if PlayerVoteFor == Index then
                VoteEvent:FireServer(Index)
                
            else]]
			--=print(MineFrame,PlayerVoteFor,Index)
			-- X print(PlayerVoteFor,Index)
			-- X AddRemoveVote:FireServer(PlayerVoteFor,Index)
			--X PlayerVoteFor = Index



			--end
			AddRemoveVote:FireServer(PlayerVoteFor,Index)
			PlayerVoteFor = Index
		end)
	if Index == 3 then
		break
	end
end
ChooseingMap.OnClientEvent:Connect(function(selectedMaps)
	print(selectedMaps)
	for Index,MapFrame in MineFrame:GetChildren() do
		local currentMap = selectedMaps[Index]
		print(MapFrame)
		MapFrame:WaitForChild("MapName",2).Text = tostring(currentMap)
		local  MapImage = MapFrame.MapImage
		local MapCloneToImageFrame : Model = currentMap:Clone()
		local CamreaTo = MapCloneToImageFrame:FindFirstChild("CameraHere")
		local viewPort  = MapImage.ViewportFrame
		local Camera = Instance.new("Camera")
		Camera.Parent = viewPort
		Camera.CFrame = CamreaTo.CFrame
		viewPort.CurrentCamera = Camera
		MapCloneToImageFrame.Parent = viewPort																																					

		if Index == 3 then
			break
		end
	end
end)
updateVote.OnClientEvent:Connect(function(Index,voteCount,OpenGui,clear)
	--    print(OpenGui)
	if OpenGui == true  then
		LocalPlayer.PlayerGui.VoteGui.Enabled = true
		--    print(OpenGui)

	elseif OpenGui == false then

		LocalPlayer.PlayerGui.VoteGui.Enabled = false
	end
	if Index and voteCount then
		LocalPlayer.PlayerGui.VoteGui.MineFrame:GetChildren()[Index]:WaitForChild("Votes").Text = voteCount 
	end
	if clear == true then
		for _,frame in LocalPlayer.PlayerGui.VoteGui.MineFrame:GetChildren() do
			frame.Votes.Text = 0
			frame.MapImage.ViewportFrame:FindFirstChildOfClass("Model"):Destroy()
		end
		PlayerVoteFor = 0
		
	end
	--end
	--script.Parent.Parent.Enabled = updatevotegui
end)



-- ServerScriptService
-- variables 
-- events 

--[[
repeat
	print("the game cant start until at least two players intered the  game, Player Count is : ",#Players:GetChildren() )
	
	wait(1)
until #Players:GetChildren() >= 2
updatedvote:FireAllClients(nil,nil,true)]]



-- MineScript 

local map
local ChooseMapsisdone = game.ReplicatedStorage:WaitForChild("chooseMaps")
local updatedvote = game.ReplicatedStorage:WaitForChild("updateVote")
local AddRemoveVote = game.ReplicatedStorage:WaitForChild('AddRemoveVote')
--------------------------------------------------------------------------------
local MapsFolder = game.ReplicatedStorage:WaitForChild("Maps")
local Players = game.Players
local StarterGui = game.StarterGui
local VoteGui = StarterGui:WaitForChild("VoteGui")
local SelcetedMaps = {}
local MapsVotes = {0,0,0}
local PlayerVoted = {}
local playersAlive = {}
local roundstarted = false
local sword = game.ReplicatedStorage:WaitForChild("ClassicSword",3)

local function FindTheMax(Table)
	local biggestValue =  Table[1]

	for _,value in Table do
		if value > biggestValue then
			biggestValue = value
		end
	end
	return biggestValue
end
local function ChooseMaps()
	local Maps = MapsFolder:GetChildren()
	for i,Map in Maps do
		local randomMap = Maps[math.random(1,#Maps)]
		if table.find(SelcetedMaps,randomMap) ~= nil then -- table.find return nil if value is not there 
			repeat
				randomMap = Maps[math.random(1,#Maps)]
			until table.find(SelcetedMaps,randomMap) == nil -- this mean the map that have been choosen is not already on the SelcetedMaps
		end
		table.insert(SelcetedMaps,randomMap)

		if #SelcetedMaps == 3 then
			break
		end
	end
end
local function MovePlayers()
	for _,player in Players:GetChildren() do
		print(player)
		table.insert(playersAlive,player)
		local hu:Humanoid = player.character:FindFirstChild("Humanoid")
		hu.StateChanged:Connect(function(old,new)
			if new == Enum.HumanoidStateType.Dead then
				local findPlayer = table.find(playersAlive,player)
				if findPlayer  then
					print(findPlayer)
					table.remove(playersAlive,findPlayer)
					print(playersAlive)
				end
			end
		end)
		local clonesword = sword:Clone()
		clonesword.Parent = player.Backpack
		for _,Stuff in map:GetChildren() do
			local SpawnFolder = map:WaitForChild("SpwanLocation"):GetChildren()
			local humanoidRootPart  = player.character:WaitForChild("HumanoidRootPart")
			for __,spwan in SpawnFolder   do
				local randomSpwaner = SpawnFolder[math.random(1,#SpawnFolder)]
				humanoidRootPart.CFrame = randomSpwaner.CFrame
				break
			end
		end
	end
end
local function GameRound()

	ChooseMaps()
	----events------

	Players.PlayerAdded:Connect(function(player)
		updatedvote:FireClient(player,nil,nil,true)
		ChooseMapsisdone:FireClient(player,SelcetedMaps)

	end)
	for _,player in Players:GetChildren() do
		updatedvote:FireClient(player,nil,nil,true)
		ChooseMapsisdone:FireClient(player,SelcetedMaps)
		--[[local voteGui = player.PlayerGui:WaitForChild("VoteGui")
		local MineFrame = voteGui.MineFrame
		for Index,Frame in MineFrame:GetChildren() do
			local MapClone =SelcetedMaps[Index]:Clone()
			MapClone.Parent = 	Frame.MapImage.ViewportFrame 
			Frame.MapImage.ViewportFrame.CurrentCamera = MapClone:WaitForChild("")

		end]]


	end
	local function VoteSystem()
	--[[VoteEvent.OnServerEvent:Connect(function(player,Index)
		if table.find(PlayerVoted,player) == nil then
			print("XXX")
			MapsVotes[Index] += 1
			updatedvote:FireAllClients(Index,MapsVotes[Index])
			table.insert(PlayerVoted,player)

		end

	end)]]


		AddRemoveVote.OnServerEvent:Connect(function(p,PlayerVoteFor,Index)
			if PlayerVoteFor == Index then
				return
			end
			if PlayerVoteFor > 0 then
				print(PlayerVoteFor,Index) -- old vote is playervotefor new is index
				MapsVotes[PlayerVoteFor] -= 1
				updatedvote:FireAllClients(PlayerVoteFor,MapsVotes[PlayerVoteFor])

			end
			MapsVotes[Index] += 1
			updatedvote:FireAllClients(Index,MapsVotes[Index])

		end)
	end
	VoteSystem()
	--- move players after chooseing a map

	task.wait(8)
	updatedvote:FireAllClients(nil,nil,false)

	local x = FindTheMax(MapsVotes)
	map = SelcetedMaps[table.find(MapsVotes,x)]:Clone()
	map.Parent = workspace
	workspace:WaitForChild(map,3)
	print(map)

	MovePlayers()

end
GameRound()
local function Clear()
	table.clear(playersAlive)
	table.clear(SelcetedMaps)
	table.clone(PlayerVoted)
	for i,value in MapsVotes do
		MapsVotes[i]= 0
	end
	print(map)
	map:Destroy()
	updatedvote:FireAllClients(nil,nil,nil,true)
	print(playersAlive,SelcetedMaps,PlayerVoted,MapsVotes)
	print("Cleared")
	GameRound()

end
while task.wait(1.5) do
	if #playersAlive == 0 then
		Clear()
		continue
	end
end



This is being connected every round. You’ll need to disconnect it once the voting ends, which you can do like this:

local connection = AddRemoteVote.OnServerEvent:Connect(function(p, PlayerVoteFor, Index)
	-- ...
end)

task.wait(8)

connection:Disconnect()
2 Likes

Oh how i didn’t thought about this, thank you bro

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.