Table doesn't clear even if it is supposed to

  1. What is the issue? The Table (NewServerTable) is supposed to clean [table.clean(NewServerTable)] in the function UndoChanges(), but when a player tries the second time, it gives the same output as if that player was still in NewServerTable, even tho it should have been removed from it. The Same happens with a player that tries to call UndoChanges() ((with the UndoChanges() call I mean to run the script until the end as how it should be ran)) he wont be added to the table, adding everybody else but the one who called UndoChanges(), and if he is the only one that is supposed to be in the table it gives the same error as if no one would be in the table.

I reposted this because now I found the real issue, the other post not being what caussed the trouble

Here’s the part of the code that I suspect of being the one that causes the trouble:

local function begincountdown()
	game.ReplicatedStorage.GetPlayerPressed.OnServerEvent:connect(function(Player)
		local player = Player
		if player then
			table.insert(NewServerTable, player) 
		end
	end)
    countdownActive = true
	for i = 15, 0, -1 do
		countdownLabel.Text = i
		wait(1)
	end
		countdownEnd = true
        countdownFinished()
end

Other Parts of the code that could also be causing the problem

local function UndoChanges()
  CNSCreatingLabel.Text = "No Server is being created"
  CreateNewServerButton.Text = "Create a Server"
  table.clear(NewServerTable)
  countdownEnd = false
  countdownActive = false
  countdownLabel.Text = "15"
  print("Undo Changes completed")
end
local function countdownFinished()
    local placeId = TeleportService:ReserveServer(13973434297)
	local success, result = pcall(function()
		return TeleportService:TeleportToPrivateServer(13973434297, placeId, NewServerTable)
	end)

	if success then
		local jobId = result
        --table.clear(NewServerTable)
        countdownActive = false
        print("Players Teleported to Server!")
        UndoChanges()
        print("Calling UndoChanges after Succes")
        print("Teleported Players!")
	else
		warn(result)
        UndoChanges()
        print("Failed to teleport players!")
	end
end
function ButtonClicked()
  if countdownActive == false and #NewServerTable == 0 then
   CNSCreatingLabel.Text = "Server is being created"
   CreateNewServerButton.Text = "Join New Server"  
   print("New server countdown started")
   begincountdown()
  elseif countdownActive == true and #NewServerTable > 0 then
   joinTable()
   --print("Player joined the ServerTable")
  end 		
end

CreateNewServerButton.MouseButton1Down:Connect(ButtonClicked)
local function joinTable()
	game.ReplicatedStorage.GetPlayerPressed.OnServerEvent:Connect(function(Player)
		if not table.find(NewServerTable, Player) then
			print("Player Added to NewServerTable")
			table.insert(NewServerTable, Player)
		else
			print("Player already in NewServerTable attempting to join it!")
		end
	end)
end

Also yes I put NewServerTable = {} at the start of the script

2 Likes

To be fair, I’ll just be playing a guessing game right now. The one I mainly suspect for causing the issue is the Server Event inside joinTable(). That is because, at least at first looks, I cannot find anything else that could potentially trigger that unexpected behavior. It seems that you’re connecting the game.ReplicatedStorage.GetPlayerPressed.OnServerEvent event every time the joinTable() function is called. This means that multiple event connections are being established every time a player clicks the CreateNewServerButton . Thus, the event handler function will be called multiple times for each event trigger, leading to possibly unexpected behavior.

To potentially fix this issue, you could move the event connection outside of the joinTable() function, preferably in a place where it is only called once. For example, you can connect the event outside of any functions or in a specific initialization (run only once) function that is only called once during the script’s lifetime. That way you could also eliminate the joinTable() function completely.

That’s my best guess.

1 Like

based of your reply I heavly modified the code, integrating the OnServerEvent function into buttonclicked, moving the table.clear as the first line in the begincountdown function and I think i found the issue but im not sure. it might be that at the start of the script, standalone local NewServerTable = {} is present which I think after the script ends and it restarts, it clears the NewServerTable but also creates another, so just like a duplicate with the same name the script chooses the first one, the one that had been cleaned and theres nothing to it. I am not a really advanced scripter so I also want to hear your opinion on this. I will also try to find a way to test this

1 Like

It seems like you have possibly identified a potential issue related to the declaration of NewServerTable as a standalone local variable at the start of the script. If the script restarts or runs multiple times, it could create a new instance of NewServerTable, resulting in the script accessing the empty table instead of the populated one.

You could ensure that the NewServerTable variable is persistent across script restarts or multiple script executions. One way to achieve this is by making a _G property, which should in turn keep the variable between script resets as it is a completely global server-side declaration, NewServerTable instead of a local variable at the start of the script.

Here is a non-tested code I just came up with:

_G.NewServerTable = _G.NewServerTable or {} -- Initializing NewServerTable if it doesn't exist in the global table, otherwise using the one that already exists

-- A bit of a modified version of the joinTable() without listening to the event inside of it, avoiding unexpected behavior and adding the global table NewServerTable
local function joinTable(player)
    if not table.find(_G.NewServerTable, player) then
        print("Player Added to NewServerTable")
        table.insert(_G.NewServerTable, player)
    else
        print("Player already in NewServerTable attempting to join it!")
    end
end

local function begincountdown()
    table.clear(_G.NewServerTable)  -- Clearing the table before populating it completely

    game.ReplicatedStorage.GetPlayerPressed.OnServerEvent:Connect(joinTable) -- Moved the Event inside here to prevent multiple events running in joinTable()
    countdownActive = true

    for i = 15, 0, -1 do
        countdownLabel.Text = i
        wait(1)
    end

    countdownEnd = true
    countdownFinished()
end

-- Rest of what you have written should remain roughly the same here, unless you are accessing the NewServerTable, then add a _G.

table.clear(_G.NewServerTable) -- Clearing the NewServerTable at the start of the script immediately (if you desire)

It would be easier to work with if I had a snippet model provided to test it myself in-game and provide adequate responses, but this is all I can think of currently.

1 Like