[NOT SOLVED] For i, loops acting weird

Ya, just wanted to point it out.

The client script looks alright tho…

I think I found out the problem…?

RemoteEvents.LeftQueue.OnServerEvent:Connect(function(Player)
	if Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == true then



		Player.StatsFolder.ReadyUp.Value = false
		Player.StatsFolder.InGame.Value = false
		if Signals.Status.Value:Find("Voting Has Began") then return end
		table.remove(InQueue, table.find(InQueue, Player.UserId))
	end
end)

Well a little, When I join and leave and join again it will say “Looking for Players” and when I Can’t leave after that…

This is the code (Btw I added a variable on the Client that says if the Player is in the queue or not.

Server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Signals = ReplicatedStorage:WaitForChild("Signals")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")

local InQueue = {}
local InGame = {}
local MaxPlayers = 1


local VotingStarted = false

local RandomMap1Var = nil
local RandomMap2Var = nil
local RandomMap3Var = 3




function PlayerJoined(Player)
	local StatsFolder = script.StatsFolder:Clone()
	StatsFolder.Parent = Player
end



function PlayerLeft(Player)
	if table.find(InQueue, Player.UserId) then
		warn("The Player has Left the game while in queue, Removing the Player from the Queue.")
		table.remove(InQueue, Player.UserId)
	end
end





function Voting()

	if (#InQueue == MaxPlayers and Signals.MatchStarted.Value == false) and VotingStarted == false then 
		VotingStarted = true


		repeat
			task.wait(.1)
			local RandomMap1 = ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			local RandomMap2 =  ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			local RandomMap3 =  ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			RandomMap1Var = RandomMap1
			RandomMap2Var = RandomMap2
			RandomMap3Var = RandomMap3

		until (RandomMap1 ~= RandomMap2) and (RandomMap3 ~= RandomMap2) and (RandomMap1 ~= RandomMap3)
		if (RandomMap1Var ~= nil and RandomMap2Var ~= nil) and RandomMap3Var ~= nil then
			RemoteEvents.VoteChoices:FireAllClients(RandomMap1Var, RandomMap2Var, RandomMap3Var)
		end


		for i = 10, 0, -1  do

			if #InQueue == MaxPlayers then
				Signals.Status.Value = "Voting Has Began (" .. i .. ")"
				RemoteEvents.VotingBegan:FireAllClients()
				task.wait(1)
			else 
				Signals.Status.Value = "Looking for Players (" .. #InQueue .. ")"
				repeat
					task.wait(.2)
				until #InQueue == MaxPlayers
				VotingStarted = false
				return Voting()
			end
		end
	end
end


RemoteEvents.QueuedUp.OnServerEvent:Connect(function(Player)
	if (Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == false) and (Signals.MatchStarted.Value == false and #InQueue <= MaxPlayers) then

		Player.StatsFolder.ReadyUp.Value = true
		Player.StatsFolder.InGame.Value = false
		table.insert(InQueue, Player.UserId)
		Signals.Status.Value = "Looking for Players (" .. #InQueue .. ")"
		Voting()
	end
end)

RemoteEvents.LeftQueue.OnServerEvent:Connect(function(Player)
	if Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == true then



		Player.StatsFolder.ReadyUp.Value = false
		Player.StatsFolder.InGame.Value = false
		if Signals.Status.Value:Find("Voting Has Began") then return end
		table.remove(InQueue, table.find(InQueue, Player.UserId))
	end
end)







game.Players.PlayerAdded:Connect(PlayerJoined)
game.Players.PlayerRemoving:Connect(PlayerLeft)   

Client

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")


local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded
local Camera = game.Workspace.Camera

local InQueue = false



local MainGui = Player.PlayerGui:FindFirstChild("MainGui")

local Map = game.Workspace.Map
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local Signals = ReplicatedStorage:WaitForChild("Signals")



RemoteEvents.VotingBegan.OnClientEvent:Connect(function()
	if InQueue then
		MainGui.VotingFrame.Visible = true
	end
end)


RemoteEvents.VoteChoices.OnClientEvent:Connect(function(RandomMap1Var, RandomMap2Var, RandomMap3Var)

	if (RandomMap1Var ~= nil and RandomMap2Var ~= nil) and (RandomMap3Var ~= nil) then
		MainGui.VotingFrame.Visible = true
		MainGui.VotingFrame.Choice1.MapName.Text = RandomMap1Var.Name
		MainGui.VotingFrame.Choice2.MapName.Text = RandomMap2Var.Name
		MainGui.VotingFrame.Choice3.MapName.Text = RandomMap3Var.Name
	end
end)


RemoteEvents.VotingStopped.OnClientEvent:Connect(function()
	MainGui.VotingFrame.Visible = false
end)

Signals.Status:GetPropertyChangedSignal("Value"):Connect(function()
	MainGui.LookingForPlayersFrame.LookingForPlayersText.Text = Signals.Status.Value
end)

function Room()
	Character.HumanoidRootPart.CFrame = Map.Room.PlayerRoom.PlayerPositionPart.CFrame
	Character.Humanoid.JumpPower = 0
	Character.Humanoid.WalkSpeed = 0

	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = Map.Room.PlayerRoom.CameraPart.CFrame
end

MainGui.QueueButton.MouseButton1Click:Connect(function()
	if (Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == false) and (Signals.MatchStarted.Value == false) then
		if InQueue == false then
			RemoteEvents.QueuedUp:FireServer(Player)
			MainGui.LookingForPlayersFrame.Visible = true
			MainGui.QueueButton.Visible = false
			MainGui.LeaveQueueButton.Visible = true
			InQueue = true
		end
	end
end)




MainGui.LeaveQueueButton.MouseButton1Click:Connect(function()
	if Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == true then
		if InQueue == true then
			RemoteEvents.LeftQueue:FireServer(Player)

			MainGui.LookingForPlayersFrame.Visible = false
			MainGui.QueueButton.Visible = true
			MainGui.LeaveQueueButton.Visible = false
			MainGui.VotingFrame.Visible = false
			InQueue = false
		end
	end
end)

Room()
1 Like

Try changing the queued event to

RemoteEvents.QueuedUp.OnServerEvent:Connect(function(Player)
	if (Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == false) and (Signals.MatchStarted.Value == false and #InQueue <= MaxPlayers) then

		Player.StatsFolder.ReadyUp.Value = true
		Player.StatsFolder.InGame.Value = false
		if not table.find(InQueue, Player.UserId) then
			table.insert(InQueue, Player.UserId)
		end
		Signals.Status.Value = "Looking for Players (" .. #InQueue .. ")"
		Voting()
	end
end)
1 Like

Almost, But now it always counts down the queue even if the player leaves Here is the voting function

function Voting()

	if (#InQueue == MaxPlayers and Signals.MatchStarted.Value == false) and VotingStarted == false then 
		VotingStarted = true


		repeat
			task.wait(.1)
			local RandomMap1 = ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			local RandomMap2 =  ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			local RandomMap3 =  ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			RandomMap1Var = RandomMap1
			RandomMap2Var = RandomMap2
			RandomMap3Var = RandomMap3

		until (RandomMap1 ~= RandomMap2) and (RandomMap3 ~= RandomMap2) and (RandomMap1 ~= RandomMap3)
		if (RandomMap1Var ~= nil and RandomMap2Var ~= nil) and RandomMap3Var ~= nil then
			RemoteEvents.VoteChoices:FireAllClients(RandomMap1Var, RandomMap2Var, RandomMap3Var)
		end


		for i = 10, 0, -1  do

			if #InQueue == MaxPlayers then
				Signals.Status.Value = "Voting Has Began (" .. i .. ")"
				RemoteEvents.VotingBegan:FireAllClients()
				task.wait(1)
			else 
				Signals.Status.Value = "Looking for Players (" .. #InQueue .. ")"
				repeat
					task.wait(.2)
				until #InQueue == MaxPlayers
				VotingStarted = false
				return Voting()
			end
		end
	end
end
1 Like

This means that the player didnt leave the queue, change the queue leaving function to have the if statement at the top, forgot to tell you to do that after.

It already does
(Chars)

Not that the one which checks the status

That one (If statement)??

Yes exactly

This text will be blurred

Now I can’t leave and the Status Text is "Loading…"

1 Like

I used CollectionService to master my voting issues. Add a tag for the player’s vote to the player, as they vote… run a timer… then count up the tags… This keeps things simple and precise. If someone leaves, no problem. The tag is gone from the service as the player object leaves the server… it’s self managing.

Also the beauty of CollectionService - those tags replicate between client and server.

I’ve never used Collection service so I don’t know anything about it…

Here is the code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Signals = ReplicatedStorage:WaitForChild("Signals")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")

local InQueue = {}
local InGame = {}
local MaxPlayers = 1


local VotingStarted = false

local RandomMap1Var = nil
local RandomMap2Var = nil
local RandomMap3Var = 3




function PlayerJoined(Player)
	local StatsFolder = script.StatsFolder:Clone()
	StatsFolder.Parent = Player
end



function PlayerLeft(Player)
	if table.find(InQueue, Player.UserId) then
		warn("The Player has Left the game while in queue, Removing the Player from the Queue.")
		table.remove(InQueue, Player.UserId)
	end
end





function Voting()

	if (#InQueue == MaxPlayers and Signals.MatchStarted.Value == false) and VotingStarted == false then 
		VotingStarted = true


		repeat
			task.wait(.1)
			local RandomMap1 = ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			local RandomMap2 =  ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			local RandomMap3 =  ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			RandomMap1Var = RandomMap1
			RandomMap2Var = RandomMap2
			RandomMap3Var = RandomMap3

		until (RandomMap1 ~= RandomMap2) and (RandomMap3 ~= RandomMap2) and (RandomMap1 ~= RandomMap3)
		if (RandomMap1Var ~= nil and RandomMap2Var ~= nil) and RandomMap3Var ~= nil then
			RemoteEvents.VoteChoices:FireAllClients(RandomMap1Var, RandomMap2Var, RandomMap3Var)
		end


		for i = 10, 0, -1  do

			if #InQueue == MaxPlayers then
				Signals.Status.Value = "Voting Has Began (" .. i .. ")"
				RemoteEvents.VotingBegan:FireAllClients()
				task.wait(1)
			else 
				Signals.Status.Value = "Looking for Players (" .. #InQueue .. ")"
				repeat
					task.wait(.2)
				until #InQueue == MaxPlayers
				VotingStarted = false
				return Voting()
			end
		end
	end
end


RemoteEvents.QueuedUp.OnServerEvent:Connect(function(Player)
	if Signals.Status.Value:Find("Voting Has Began") then return end
	if (Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == false) and (Signals.MatchStarted.Value == false and #InQueue <= MaxPlayers) then

		Player.StatsFolder.ReadyUp.Value = true
		Player.StatsFolder.InGame.Value = false
		if not table.find(InQueue, Player.UserId) then
			table.insert(InQueue, Player.UserId)
		end
		Signals.Status.Value = "Looking for Players (" .. #InQueue .. ")"
		Voting()
	end
end)

RemoteEvents.LeftQueue.OnServerEvent:Connect(function(Player)
	if Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == true then



		Player.StatsFolder.ReadyUp.Value = false
		Player.StatsFolder.InGame.Value = false
		if Signals.Status.Value:Find("Voting Has Began") then return end
		table.remove(InQueue, table.find(InQueue, Player.UserId))
	end
end)







game.Players.PlayerAdded:Connect(PlayerJoined)
game.Players.PlayerRemoving:Connect(PlayerLeft)   
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")


local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded
local Camera = game.Workspace.Camera

local InQueue = false



local MainGui = Player.PlayerGui:FindFirstChild("MainGui")

local Map = game.Workspace.Map
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local Signals = ReplicatedStorage:WaitForChild("Signals")



RemoteEvents.VotingBegan.OnClientEvent:Connect(function()
	if InQueue then
		MainGui.VotingFrame.Visible = true
	end
end)


RemoteEvents.VoteChoices.OnClientEvent:Connect(function(RandomMap1Var, RandomMap2Var, RandomMap3Var)

	if (RandomMap1Var ~= nil and RandomMap2Var ~= nil) and (RandomMap3Var ~= nil) then
		MainGui.VotingFrame.Visible = true
		MainGui.VotingFrame.Choice1.MapName.Text = RandomMap1Var.Name
		MainGui.VotingFrame.Choice2.MapName.Text = RandomMap2Var.Name
		MainGui.VotingFrame.Choice3.MapName.Text = RandomMap3Var.Name
	end
end)


RemoteEvents.VotingStopped.OnClientEvent:Connect(function()
	MainGui.VotingFrame.Visible = false
end)

Signals.Status:GetPropertyChangedSignal("Value"):Connect(function()
	MainGui.LookingForPlayersFrame.LookingForPlayersText.Text = Signals.Status.Value
end)

function Room()
	Character.HumanoidRootPart.CFrame = Map.Room.PlayerRoom.PlayerPositionPart.CFrame
	Character.Humanoid.JumpPower = 0
	Character.Humanoid.WalkSpeed = 0

	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = Map.Room.PlayerRoom.CameraPart.CFrame
end

MainGui.QueueButton.MouseButton1Click:Connect(function()
	if (Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == false) and (Signals.MatchStarted.Value == false) then
		if InQueue == false then
			RemoteEvents.QueuedUp:FireServer(Player)
			MainGui.LookingForPlayersFrame.Visible = true
			MainGui.QueueButton.Visible = false
			MainGui.LeaveQueueButton.Visible = true
			InQueue = true
		end
	end
end)




MainGui.LeaveQueueButton.MouseButton1Click:Connect(function()
	if Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == true then
		if InQueue == true then
			RemoteEvents.LeftQueue:FireServer(Player)

			MainGui.LookingForPlayersFrame.Visible = false
			MainGui.QueueButton.Visible = true
			MainGui.LeaveQueueButton.Visible = false
			MainGui.VotingFrame.Visible = false
			InQueue = false
		end
	end
end)

Room()

Its super easy to use. Maybe I’ll write up an example of how you could use it, with your code above… after I finish cooking up some dinner. :slight_smile:

1 Like

Can you show me an example now?

Hey, sorry for not replying, I went to sleep…

1 Like

Back at my computer after a night’s sleep. I’ll post your original code (from the first post), modified with my recommendation on it now. Haven’t tested the code as I have no working setup for the script.

local IntermissionTime = 10

function Voting()

	if (#InQueue == MaxPlayers and Signals.MatchStarted.Value == false) and VotingStarted == false then 
		VotingStarted = true


		repeat
			task.wait(.1)
			local RandomMap1 = ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			local RandomMap2 =  ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			local RandomMap3 =  ReplicatedStorage.Maps:GetChildren()[math.random(1, #ReplicatedStorage.Maps:GetChildren())]
			RandomMap1Var = RandomMap1
			RandomMap2Var = RandomMap2
			RandomMap3Var = RandomMap3

		until (RandomMap1 ~= RandomMap2) and (RandomMap3 ~= RandomMap2) and (RandomMap1 ~= RandomMap3)
		if (RandomMap1Var ~= nil and RandomMap2Var ~= nil) and RandomMap3Var ~= nil then
			RemoteEvents.VoteChoices:FireAllClients(RandomMap1Var, RandomMap2Var, RandomMap3Var)
		end
		
		local Timer = IntermissionTime

		while Timer > 0 do

			if #InQueue == MaxPlayers then
				Signals.Status.Value = "Voting Has Began (" .. Timer .. ")"
				RemoteEvents.VotingBegan:FireAllClients()
				task.wait(1)
				Timer -= 1
			else 
				Timer = IntermissionTime -- Reset timer if player left, or player count hasn't reached max.
				for i=1, 10 do
					Signals.Status.Value = "Looking for Players  (" .. #InQueue .. ")"
					if #InQueue == MaxPlayers then  break else RemoteEvents.VotingStopped:FireAllClients() end
					task.wait(1)
				end
				if #InQueue == MaxPlayers then
					VotingStarted = false
					return Voting()
				end
			end
		end
		VotingStarted = true
		Signals.MatchStarted.Value = true
		Signals.Status.Value = "A Match is Already in The Proccess Please Wait.."
		RemoteEvents.VotingStopped:FireAllClients()
	end
end

Still having the same issue.
Also, it’s not collection service…