Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

When i use my local variable in a function, why does it change it for the entire server?

Can I pls get some closure? THE PROBLEM IS IN LINE 10

Here is the script:

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"
local inQueue = false

script.Parent.Touched:Connect(function(TouchedPart)
	if TouchedPart.Parent:FindFirstChild("Humanoid") and not inQueue then
		inQueue = true
		local Character = TouchedPart.Parent
		local Player = game.Players:GetPlayerFromCharacter(Character)
		table.insert(Table,Player)
		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)

1 answer

Try turning it into a table or maybe putting the variable in the function would work

Script:

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)
    local inQueue = false
    if TouchedPart.Parent:FindFirstChild("Humanoid") and not inQueue then
        inQueue = true
        local Character = TouchedPart.Parent
        local Player = game.Players:GetPlayerFromCharacter(Character)
        table.insert(Table,Player)
        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)

4 comments

0

I put the variable into the funtion and it addded the player into the table multiple times

0

Yeah, I realized my error and don't do that lol. Could you explain the issue a little more?

0

It basically makes the variable true for the entire lobby when the function is fired off.

0

Where is this script located?

This discussion is preserved as it was. New answers, comments, and votes are closed.