Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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")
game.Workspace.Pad1.CanTouch = true
game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "PLAYERS: 0/2"
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
                game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "PLAYERS: 1/2"
                repeat
                    wait(0.01)
                until #Table > 1
            else
                if #Table > 1 then
                    game.Workspace.TwoPlayerPlayers.Value = 1
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "PLAYERS: 2/2"
                    wait(2)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 10"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 9"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 8"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 7"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 6"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 5"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 4"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 3"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 2"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 1"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING NOW"
                    wait(2)
                    game.Workspace.TwoPlayerPlayers.Value = 0
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "PLAYERS: 0/2"
                    Table = {}
                end
            end
        end
    end
end)

1 answer

Log in to vote
0
Answered by 2 years ago

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")
game.Workspace.Pad1.CanTouch = true
game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "PLAYERS: 0/2"
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
                game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "PLAYERS: 1/2"
                repeat
                    wait(0.01)
                until #Table > 1
            else
                if #Table > 1 then
                    game.Workspace.TwoPlayerPlayers.Value = 1
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "PLAYERS: 2/2"
                    wait(2)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 10"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 9"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 8"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 7"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 6"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 5"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 4"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 3"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 2"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING IN: 1"
                    wait(1)
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "TELEPORTING NOW"
                    wait(2)
                    game.Workspace.TwoPlayerPlayers.Value = 0
                    game.Workspace.TWOPLAYERQUEUE.Overhead.BillboardGui.Frame["PLAYERS&TELEPORTING"].Text = "PLAYERS: 0/2"
                    Table = {}
                end
            end
        end
    end
end)
0
JESUS CHRIST THANK YOU YOU ARE A HERO VictoreRoyale667 8 — 2y
Ad

Answer this question