[NOT SOLVED] For i, loops acting weird

So I was making a voting system But I’ve ran into a Problem I’ve made a countdown But a problem appeared basically when the player joins a queue and leaves it at 5 seconds (any number, it’s just an example) and then joins sometimes the loop will count from 5 to 0 instead of 10 to 0
Here is my Code

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 
				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
2 Likes

Can someone help it has been 1 Hour.

1 Like

I don’t really know why it isn’t working, but where is the variable MaxPlayers defined? Is it in another part of the script?

1 Like

MaxPlayers is a variable

local MaxPlayers = 1
1 Like

Ok, did you try printing parts of the script to find the problem? It might help finding the issue.

1 Like

I did, the prints didn’t really help at all.

1 Like

I’m not a very good scripter, so I’m not sure if I can manage to solve this problem, but what is Signals.MatchStarted? It could be a small mistake related to it.

1 Like

Basically when this value becomes true the Client does something.
Basically a remote event but not really since it doesn’t have params…

Wait, how can a remote event be a value? Also, what is it parented to? Is the script that does something on the client a server script or a local one?

This isn’t the problem man, Basically the Client knowns if the match started or not by this value.

Told you, I’m not a good scripter, but I was just asking because local scripts can’t access ServerStorage and ServerScriptService, so if your checking the value in one of those, it wont work. Sorry, but I probably can’t solve this problem.

It’s in replicated storage and Everybody starts from somewhere so you’re not bad.

Wait, I might have found the reason. It might be those 2 for loops after each other.

When the else part loop starts, there is still the other part of the loop.

Edit: Sorry I quoted the wrong part, I just fixed it right now.

I tried that, it didn’t work…

I didn’t give any code, I was just quoting the part that might be the problem. Anyways, if this won’t work, sorry but I can’t help. Hopefully someone better at scripting will help you.

1 Like

I know you didn’t give, But thanks for the help anyway!

1 Like

You’re right. If the top loop were to run 10 times and it goes by the “else” 10 times then the next for loop will be run 10 times too and the code inside that second for loop 100 times. I don’t think that’s supposed to happen.

1 Like

It doesn’t work.

Could you go through and explain what each part of your code does? Why are you checking if the total InQueue equals MaxPlayers 10 times? Is this some sort of countdown? And why run another for loop inside 10 times?

it’s a bit hard to explain but I’ll explain Basically the player queues up it will add them to the table called “InQueues” when each player queues up we connect to the voting function in the voting function we check if the players in the queue table is equal to the Max players and the game hasn’t started a match, etc
The problem is the player can exit the queue while in the voting session that’s why I’m doing that.