[NOT SOLVED] For i, loops acting weird

At the end of the provided code, try changing votingStarted to false

1 Like

Nope, it didn’t work.

1 Like

@msix29 @Cens_r Here is my entire script.

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 
				repeat
					task.wait(.2)
				until #InQueue == MaxPlayers
				VotingStarted = false
				return Voting()
			end
		end
		VotingStarted = false
		Signals.MatchStarted.Value = true
		Signals.Status.Value = "A Match is Already in The Proccess Please Wait.."
		RemoteEvents.VotingStopped:FireAllClients()
	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
		table.remove(InQueue, table.find(InQueue, Player.UserId))
	end
end)







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

Is this a matchmaking system which teleports you to another game / place?

1 Like

Nope same game.

1 Like

You’re better off using a separate variable to count the timer down. If a player leaves and the server is now less than the max, reset that variable back to ten seconds. I cannot provide code as I am using a phone, sorry.

You simply use a while true do loop instead, and when the player count isn’t equal to max, you can reset the timer. If the timer has hit zero and the server is still maxed, use break to escape the while loop.

In the leaving queue part, add this at the top

if Signals.Status.Value:Find("Voting Has Began") then return end
1 Like

It now breaks the entire system, First when I join the queue it works but when I leave the Voting Frame becomes visible and I can’t join the queue.

Here is the Local script code

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 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()
	MainGui.VotingFrame.Visible = true
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
		RemoteEvents.QueuedUp:FireServer(Player)
		MainGui.LookingForPlayersFrame.Visible = true
		MainGui.QueueButton.Visible = false
		MainGui.LeaveQueueButton.Visible = true
	end
end)
1 Like

Since they will be looking for a map, you wont be able to join until that status changes to something other than voting for map…

1 Like

Oh, then what Could I do to fix that?

The player needs to wait until the status changes and after that he can queue again.

1 Like

Well then there are 0 players in the queue…
I don’t know what to do to be honest.

I’m lost.

Change the for loop to this

		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

it’s doing the same thing, Here is the script in case if its updated.

Local

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 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()

		MainGui.VotingFrame.Visible = true
	
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
		RemoteEvents.QueuedUp:FireServer(Player)
		MainGui.LookingForPlayersFrame.Visible = true
		MainGui.QueueButton.Visible = false
		MainGui.LeaveQueueButton.Visible = true
	end
end)




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

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

Room()

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 Signals.Status.Value:Find("Voting Has Began") then return end
	if Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == true then



		Player.StatsFolder.ReadyUp.Value = false
		Player.StatsFolder.InGame.Value = false
		table.remove(InQueue, table.find(InQueue, Player.UserId))
	end
end)







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

try doing that instead of 1,10

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)

Remove the Voting()
Add it at the end of the whole script, the voting will just call itself without remotes, you can have another remote which fires when the round ends calling this function again.

Sadly, it still does the same thing…
I think its the Client problem Can you check the Local script?

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 Signals.Status.Value:Find("Voting Has Began") then return end
	if Player.StatsFolder.InGame.Value == false and Player.StatsFolder.ReadyUp.Value == true then



		Player.StatsFolder.ReadyUp.Value = false
		Player.StatsFolder.InGame.Value = false
		table.remove(InQueue, table.find(InQueue, Player.UserId))
	end
end)







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

What do you mean by nothing?

:FireServer(Player)

No need to have the Player as a variable, the OnServerEvent has it as a built-in parameter.

Yeah, that’s true but it doesn’t really fix the code.