0

# My function keeps repeating and turning the inQueue variable back to false?

This is how the problem is happening. When i touch the part it runs this function multiple times. And I have a problem with the "inQueue" variable. I know that it is setting iot back to false and that is the problem but i do not know where to set it to true and false.

Here is my code. (THE PROBLEM IS HAPPENING BETWEEN LINES 7-17:

local Table = {}
local MaxPlayers = 2
local ts = game:GetService("TeleportService")
script.Parent.Touched:Connect(function(TouchedPart)
if TouchedPart.Parent:FindFirstChild("Humanoid") and #Table < 2 then
local inQueue = false
if not inQueue then
inQueue = true
local Character = TouchedPart.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
table.insert(Table,Player)
print(#Table)
print(#Table)
print(script.Parent.CanTouch)
print(inQueue)
if #Table < 2 then
repeat
wait(0.01)
until #Table > 1
else
if #Table > 1 then
game.Workspace.TwoPlayerPlayers.Value = 1
wait(2)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(2)
game.Workspace.TwoPlayerPlayers.Value = 0
Table = {}
end
end
end
end
end)



0

The InQue variable is being made separately for each time the function is reran, this means that: - The player touches it, a new variable is made in that function. - If the player touches it again, it will create that variable again. So therefore, it is made every time the function is made and will be false. To fix this, simply move the line local inQueue = false and then delete it from inside the function (Line 8 in your code) to the start so the start will be:

local Table = {}
local MaxPlayers = 2
local ts = game:GetService("TeleportService")
local inQueue = false


However, if you're using a server script [normal script] there is another problem... This will affect every player. There might be better ways to fix this, but this is a way I am going to show it.

You can fully delete the InQueue variable, it's not needed. You can instead do this: On line 9 where it says if not inQueue then, you can replace this to check if the player exists in the table, however the player needs to be defined before that. So simply, it can become this:

-- Code here
local Character = TouchedPart.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
if not table.find(Table, Player) then
-- Code here


So, all this basically does that is different is creates the Character and Player variable slightly earlier in the code, and then rather than checking a Boolean variable, it checks if the player is not in the table, you put them in for the que.

• Player touches the part
• Part checks if the player has already touched it, and is in the table
• If not, it puts the player into the table, so it won't run again for that player.

The server script at the end should be:

local Table = {}
local MaxPlayers = 2
local ts = game:GetService("TeleportService")
script.Parent.Touched:Connect(function(TouchedPart)
if TouchedPart.Parent:FindFirstChild("Humanoid") and #Table < 2 then
local Character = TouchedPart.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
if not table.find(Table, Player) then
table.insert(Table,Player)
print(#Table)
print(#Table)
print(script.Parent.CanTouch)
if #Table < 2 then
repeat
wait(0.01)
until #Table > 1
else
if #Table > 1 then
game.Workspace.TwoPlayerPlayers.Value = 1
wait(2)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(1)
wait(2)
game.Workspace.TwoPlayerPlayers.Value = 0