Cannot Remove Table Array After Adding

Hello there! I am having an issue with removing table arrays after a player has died, and was curious to know if anybody has a solution to my code?

The issue occurs on the second round in of my game I am making, when it is supposed to check for the winner (there should only be one table array left). There are no errors, but when a player dies in the second round, no winner shows up and the game moves on because there is more than one array still left.

I have looked around to see if I could find a solution but for what I am trying to accomplish, I could not find an exact solution.

Server Script Code For Removing The Table:


local playersInGame = {

	["In"] = {},
	
	["Out"] = {},
}
Players.PlayerAdded:Connect(function(Player)
Char.Humanoid.Died:Connect(function()	
	for i,v in pairs(playersInGame["In"]) do
		table.remove(playersInGame["In"], tonumber(table.find(playersInGame["In"], i)))
	end
	Player.Team = game.Teams.Out
	table.insert(playersInGame["Out"], Player.Name)
	wait(1)
	Char.Humanoid.JumpPower = 0
end)
end)

Server Script Code For Adding The Table:

function SetupTeams()
	for i,v in pairs(game.Players:GetPlayers()) do
		v.Team = game.Teams.In
		table.insert(playersInGame["In"],#playersInGame["In"]+1, v.Name)
		print(unpack(playersInGame["In"])) --Was Just For Testing
	end
end

Module Script To Check Table:

for i,v in pairs(playersInGame) do
		print(#playersInGame["In"])
		if #playersInGame["In"] == 1 then
			
			print("tried to check winner")
			local Winner = playersInGame["In"][1]
			local WinnerChar = game.Players[Winner].Character or game.Players[Winner].CharacterAdded:wait()
			print(unpack(playersInGame["In"]))
			Status.Value = Winner.." won the game!"
			wait(4)
			Status.Value = ""
			WinnerChar.Humanoid.JumpPower = 50
			wait(0.1)
			WinnerChar.Humanoid.Jump = true
			wait(0.1)
			WinnerChar.Humanoid.Health = 0
			wait(0.1)
			WinnerChar.Humanoid.JumpPower = 0
			wait(0.5)
			ResetChairs()
		elseif #playersInGame["In"] > 1 then
			for i,v in pairs(game.Players:GetPlayers()) do
				local AllChars = v.Character or v.CharacterAdded:wait()
				if v.Character.Humanoid.Sit == true then
					v.Character.Humanoid.JumpPower = 50
					wait(0.1)
					v.Character.Humanoid.Jump = true
					wait(0.1)
					v.Character.Humanoid.JumpPower = 0
				end
			end
	wait(.1)
			ResetChairs()
			SetupTeams()
			TPandSetChairs()
			StartRnd()
		end
	end
end

For anybody has any ideas or finds a fix, thank you so much!

It looks like you’re removing all players from ā€œInā€ when one player dies.

Instead try this:

local num = table.find(playersInGame["In"], Player.Name)
if num ~= nil then
      table.remove(playersInGame["In"], num)
end
-- Other Code here

table.find will return a number unless it can’t find what you’re looking for.

It’s really weird, I tried what you put but it did not work. It just did the same.

image That’s what was printed after the second round.

1 Like

Is the prints coming from the Module Script to check table?

One of these prints are coming from the server script. But I just realized there are multiple of these so one of the prints are also in the module script.

This is from the server script:
image

This is from the module script:

Alright, in the mean time try adding an extra print in temporarily to both and the text can be anything so long as it allows us to see the difference between the two.

Also, is the out table being reset at the start of each round?

Ah, um one more thing, is the module script that checks the table suppose to be in a for loop, looking at the prints I got thinking that SetupTeams() is somehow being called twice.

At the moment, the out table only is supposed to add a player name to it when they die, nothing else. Also, I forgot to tell you that there is another print under the ā€˜SetupTeams’ function, so I re-tested it.

Not sure if this will help since it is a bit messy, but here is what it outputted.

image The error on line 23 was just because I typed a print wrong.

I’ll check about the SetupTeams() printing twice.

Alright so, it just occurred to me that there is another line calling ā€˜SetupTeams()’, but I don’t know if it’s any help.

This is in the server script, but I thought the SetupTeams() should be put in the while loop for the first round?
image

In the module script, if there are still two or more players in the game, go through some functions again:

image

1 Like

I don’t know if this will break your game or not but try commenting out SetupTeams() in that second screenshot you sent. (the module script)

Also, this is just something that bugs me since I took a computer science class, completely optional, perhaps if you can, combine this Routine into one function that can be called by both the loop and the module script

Ex create a function called StartRoutine() and call all these function in. Then call StartRoutine in the loop and module script instead of all these functions individualy.

Edit: Also the main loop, is there anything pausing that?

I am kind of confused that you say put all the functions into ā€˜StartRoutine()’. Do I remove the functions and place everything from what was inside of them into ā€˜StartRoutine()’? Or do I just keep the functions but place them in there? Also, there shouldn’t be anything stopping the loop, I don’t think so.

Ex.

StartRoutine()
function 1()
end
function 2()
end
end

OR

StartRoutine()
–CODE–
end

1 Like

Perfectly fine to be confused, it’s a bit complicated. here’s what the StartRoutine function should look like.

function StartRoutine()
     WaitForPlayers()
     BeginIntermission()
     SetupTeams()
     TPandSetChairs()
     StartRnd()
end

This can then be called instead of having to call each function individually. Which also helps cut down on lines of code.

I myself am actually a bit confused.
the while true do loop will constantly run, but if a wait is not added it is suppose to timeout due to looping too fast.

When I imagine a round system I imagine a loop that is doing a wait at first and checking for the amount of players, once met the loop starts the game and then waits till the game is finished before going into intermission and starting the game again.

It appears that’s what’s going on here, just in different functions. Please correct me if I am wrong.

Edit: Fun fact, your Computer is constantly running a loop!

You are correct, that is what it should do. But it just occurred to me, is the loop I made waiting for the current round to be finished? Or does it keep going on behind the scenes?!

I will also make the StartRoutine() right now.

1 Like

It appears that it should constantly be running and not waiting. meaning it should time out, but at the same time if it’s not you’d think it would be constantly putting the same players into a round, basically stuck and the round unable to start. So I don’t really know.

1 Like

Alright, that’s ok if you are stuck. I have to get off at the moment, but I really appreciate you trying to help me solve this, it means a lot! :slight_smile:

1 Like

Not a problem, It may come down to having to redo some of your code or even just taking an even closer look. I’d personally start with looking at that main loop and analyzing each function.

I’m actually getting tired right now, It’s 2:18 AM for me currently. I’ll try and help solve this later if someone else isn’t able to help you get it solved.

Alright, thank you! Have a good night!

I FOUND THE SOLUTION!! I had to remove the arrays from the table (using the code I had) in the module script for after a player has won. Thank you for your help, @helperobc

1 Like