How do I fix this Table Issue?

Hi,

So i was wondering how do i remove Players from a table? because when i do it, i expect it to remove the players from the table, but does nothing to the table, but continues to add them and gives some people more EXP, How do i fix this?

Note:
In This Script, there are comments to help you, to be more specific GetPlayers and RemovePlayers functions. They are marked as “Issue Code” with a Comment

Full Code
GD = require(game:GetService("ReplicatedStorage").GlobalDictionary)


Player = game:GetService("Players")
TweenService = game:GetService("TweenService")
ReplicatedStorage = game:GetService("ReplicatedStorage")
SoundService = game:GetService("SoundService")
Audio = workspace.Audio
ServerStorage = game:GetService("ServerStorage")
MapGui = game:GetService("StarterGui").MapGui.MainFrame
MapsStorage = ServerStorage.MapStorage

AlivePlayers = GD.AlivePlayers

IntermissionTime = ReplicatedStorage.Intermission.Value;
MaxTime = IntermissionTime * 10 -- I dont think i use this anymore
StatusValue = ReplicatedStorage.StatusValue
LoadTransparency = ReplicatedStorage.LoadTransparency
MusicVolume = ReplicatedStorage.MusicVolume
ColorValue = ReplicatedStorage.ColorValue
WaveValue = ReplicatedStorage.Wave

ActiveIntermission = true
RewardBonus = 1

Minutes = GD.WaveLengthInMinutes
Seconds = GD.WaveLengthInSeconds

local TeleportPos

local Maps = {
	MapsStorage.Map1;
	MapsStorage.Map2;
}


local function SetMap()
	
	for _, map in pairs(MapsStorage:GetChildren()) do
		local RM = Maps[math.random(1, #Maps)]
		
		local A = RM:Clone() A.Parent = workspace.Map_Workspace
		
		local MapName = A.Settings.MapName.Value
		local Difficulty = GD.Difficulties[A.Settings.Difficulty.Value]
		local Creator = A.Settings.Creator.Value
		local DifficultyColor = GD.DifficultyColors[A.Settings.Difficulty.Value]

		if A.Settings.Difficulty.Value > #GD.Difficulties then
			warn("Difficulty Value Exceed's Max, Automatically Set to Extreme")
			
			ColorValue.Value = GD.DifficultyColors[4]
			StatusValue.Value = MapName.." ["..GD.Difficulties[4].."]: ".."by "..Creator
		elseif A.Settings.Difficulty.Value < 1 or nil then
			warn("Difficulty Value Lower than Minimum or nil, Automatically Set to Easy")
			
			ColorValue.Value = GD.DifficultyColors[1]
			StatusValue.Value = MapName.." ["..GD.Difficulties[1].."]: ".."by "..Creator
		else
			StatusValue.Value = MapName.." ["..Difficulty.."]: ".."by "..Creator
			ColorValue.Value = DifficultyColor
		end
		print("Map Chosen: "..MapName)
ActiveIntermission = false

		break
	end
	
	
end



function GetPlayers() -- Issue Code 1
	for _,Players in pairs(game.Players:GetPlayers()) do
		table.insert(AlivePlayers, Players)
		print("Players in game:",AlivePlayers) -- Just a test print
		
		print(#AlivePlayers) -- same with this
		
		Players.Character.Humanoid.Died:Connect(function()
			table.remove(AlivePlayers)
			print("died")
			
			warn("Status Changed:",AlivePlayers)
			print(#AlivePlayers)
		end)
		for _,Map in pairs(workspace.Map_Workspace:GetChildren()) do
			TeleportPos = Map.SpawnPoint
			if Map ~= nil then
				Players.Character:MoveTo(TeleportPos.Position + Vector3.new(0,2,0))	

			end
			end
	end
end

function AwardPlayers()
	for _,Player in pairs(AlivePlayers) do
		
Player:WaitForChild("SavedStats").EXP.Value += 50 * RewardBonus
	
	end
end

function RemovePlayers() -- Issue Code 2
	for _,Player in pairs(AlivePlayers) do

		for _,Map in pairs(workspace.Map_Workspace:GetChildren()) do
			Player.Character.Humanoid.Health = 100
			TeleportPos = workspace.SpawnLocation
			if Map ~= nil then
				Player.Character:MoveTo(TeleportPos.Position + Vector3.new(0,2,0))	
table.remove(AlivePlayers)
			end
		end

	end
end

local function RemoveMap()
	for i, Map in pairs(workspace.Map_Workspace:GetChildren()) do
		if Map ~= nil then
		Map:Destroy()
		print("Map Destroyed")
			break
		end
	end
end
while true do
local Timer = IntermissionTime
	MaxTime = IntermissionTime * 10

for i = 1, IntermissionTime do
		wait()
		Timer -= 1
		Audio.Main.Tick_Intermission:Play()
		StatusValue.Value = "Intermission: "..Timer
	
	end
	wait(1)
	if ActiveIntermission == true then
	RemoveMap()
	SetMap()
	end
WaveValue.Value += 1
	Minutes = GD.WaveLengthInMinutes
	Seconds = GD.WaveLengthInSeconds
	
	wait(5)
	StatusValue.Value = "Game Starting"
	wait(2)
	LoadTransparency.Value = 0
	ColorValue.Value = Color3.fromRGB(12,12,25)
	wait(1.5)
	MusicVolume.Value = 0.5
	wait(1.5)
	StatusValue.Value = "Wave "..WaveValue.Value..": "..GD.WaveLengthInMinutes..":00"
	LoadTransparency.Value = 1
	GetPlayers()
	wait(1)
	
repeat wait(1)
		
if #AlivePlayers == 0 then
warn("Game Over, All Players Are Dead..")
			StatusValue.Value = "Game Over"
			ActiveIntermission = true
			WaveValue.Value = 0
			RewardBonus = 0
			MusicVolume.Value = 0
			LoadTransparency.Value = 0
			wait(4)
			RemovePlayers()
			LoadTransparency.Value = 1
			wait(1)
	wait(5)
	break
end

		
			if Seconds <= 0 then
			Minutes -= 1
			Seconds = 59
		else
			Seconds -= 1
		end
		if Seconds <= 9 then
			StatusValue.Value = "Wave "..WaveValue.Value..": "..tostring(Minutes)..":0"..tostring(Seconds)
		else
			StatusValue.Value = "Wave "..WaveValue.Value..": "..tostring(Minutes)..":"..tostring(Seconds)
		end
	until Minutes <= 0 and Seconds <= 0
	wait(1)
if Minutes == 0 and Seconds == 0 then
		warn("Game Over, Survivor's Win!")
		StatusValue.Value = "Game Over"
		AwardPlayers()
		wait(3)
		MusicVolume.Value = 0
		LoadTransparency.Value = 0
		RewardBonus += .2
		wait(4)
		RemovePlayers()
		table.remove(AlivePlayers)
		LoadTransparency.Value = 1
		wait(1)
		
		
		
end		
	end
ModuleScript (In Case you are Wondering)
local GlobalDictionary = {
	
TweenService = game:GetService("TweenService");

Difficulties = {
	[1] = "Easy";
	[2] = "Medium";
	[3] = "Hard";
	[4] = "Extreme";
	
	};
DifficultyColors = {
	[1] = Color3.fromRGB(0,255,150);
	[2]	= Color3.fromRGB(208, 208, 0);
	[3]	= Color3.fromRGB(255,0,100);
	[4]	= Color3.fromRGB(200,0,0);
		
	};
	
	AlivePlayers = {};
	WaveLengthInMinutes = 1; -- This is short due to testing
	WaveLengthInSeconds = 0
	
}

return GlobalDictionary

To make your life easier:
Issue Code 1:

function GetPlayers() -- Issue Code 1
	for _,Players in pairs(game.Players:GetPlayers()) do
		table.insert(AlivePlayers, Players)
		print("Players in game:",AlivePlayers) -- Just a test print
		
		print(#AlivePlayers) -- same with this
		
		Players.Character.Humanoid.Died:Connect(function()
			table.remove(AlivePlayers)
			print("died")
			
			warn("Status Changed:",AlivePlayers)
			print(#AlivePlayers)
		end)
		for _,Map in pairs(workspace.Map_Workspace:GetChildren()) do
			TeleportPos = Map.SpawnPoint
			if Map ~= nil then
				Players.Character:MoveTo(TeleportPos.Position + Vector3.new(0,2,0))	

			end
			end
	end
end

Issue Code 2:

function RemovePlayers() -- Issue Code 2
	for _,Player in pairs(AlivePlayers) do

		for _,Map in pairs(workspace.Map_Workspace:GetChildren()) do
			Player.Character.Humanoid.Health = 100
			TeleportPos = workspace.SpawnLocation
			if Map ~= nil then
				Player.Character:MoveTo(TeleportPos.Position + Vector3.new(0,2,0))	
table.remove(AlivePlayers)
			end
		end

	end
end

Am i doing something wrong?
If you can help, that would be nice,
Thanks.

Not Really sure what to do, i have tried different things but to no prevail.

I think table.remove is missing a parameter, you only input the table but not which index to remove

1 Like

not exactly, it works as intended but it doesnt remove the items inside the table when removing the player from the map, it only works when the Player is killed

so if im interpreting this correctly, then table.remove only works in the getplayers function and not the removeplayers function correct?

does it teleport the player in the removeplayer function without having the table.remove function?

To better explain:

  • Both GetPlayers and RemovePlayers have the issue of Not removing the Player and Adding up the Player more than once, causing the player to get way more points

  • It Teleports the Player with or Without table.remove()

if the problem is not removing the player, which I assume is not properly clearing the table (sorry im not understanding this very well) then I guess a better function you could use is table.clear

Doesnt appear to fix the issue, it still doesnt remove the Items

maybe its a problem with adding multiple times, you could add a table.find check to see if the players are already in the list

After looking at it again, it appears to work, but it does this:

 21:06:25.922  Game Over, Survivors Win!  -  Server - MainScript:195 -- Previous Round ends
  21:06:32.939  {}  -  Server - MainScript:114 -- Print of the Table after table.clear
  21:06:47.021  Players in game:  ▶ {...}  -  Server - MainScript:77 -- Table of the Players gets added
  21:06:47.023  1  -  Server - MainScript:79 -- Number of Players in the match
  21:06:49.673  died  -  Server - MainScript:83 -- I Reset Here
  21:06:49.673  Status Changed: {}  -  Server - MainScript:85 -- Checks Table for changes after death
  21:06:49.673  0  -  Server - MainScript:86 -- New Number of Players in the match
  21:06:49.673  died  -  Server - MainScript:83
  21:06:49.673  Status Changed: {}  -  Server - MainScript:85 -- Repeats the same (Issue)
  21:06:49.673  0  -  Server -

Is this just the for loop for is it still there?

It only happens when another round starts

Off Topic:
This is a 2 day project so i may have made plenty of mistakes

does it not add the players that are still alive in the GetPlayers function? that’s strange behavior

When you call table.remove without a second argument, then it will remove the last item in the table, not the specific player you’re currently looping over.

I would instead use the player as an index of AlivePlayers like so: AlivePlayers[player] and remove the player from the table with AlivePlayers[player] = nil. However, you will no longer be able to access the length property with #.

I have tried this, but i would get this error:
ServerScriptService.MainScript:82: invalid argument #1 to 'remove' (table expected, got nil)

Plus, this isnt really my issue, my issue is that it keeps adding up more when adding the Player to the table when i new round starts

so instaead of just adding the Player once, everytime a new round starts, it continues to add duplicates of the player

table.insert should be replaced with AlivePlayers[player] = true, and table.remove should be replaced with AlivePlayers[player] = nil.

If you really want to use table.insert/remove then you need to add a second parameter to remove like this: table.remove(AlivePlayers, table.find(AlivePlayers, player)).

2 Likes

I tried this out again, this time it broke my teleportation, plus gave me errors about trying to index table with bool

I believe the original problem is that you are messing with the order of the table with looping over it. This alone can cause unexpected behavior.

To loop over the players in the list in this fashion, the loop code would become for player in pairs(AlivePlayers), notice the missing _.

This is what im getting: My script is working, its just when i die, it adds up depending how many Waves i go through

 Players in game:  ▼  {
                    [1] = xGOA7x
                 }  -  Server - MainScript:78
  21:41:43.957  1  -  Server - MainScript:80
  21:41:46.541  died  -  Server - MainScript:85 -- Below is my issue:
  21:41:46.541  Status Changed: {}  -  Server - MainScript:87
  21:41:46.541  0  -  Server - MainScript:88
  21:41:46.541  died  -  Server - MainScript:85
  21:41:46.541  Status Changed: {}  -  Server - MainScript:87
  21:41:46.541  0  -  Server - MainScript:88
  21:41:46.541  died  -  Server - MainScript:85
  21:41:46.541  Status Changed: {}  -  Server - MainScript:87
  21:41:46.541  0  -  Server - MainScript:88
  21:41:46.541  died  -  Server - MainScript:85
  21:41:46.542  Status Changed: {}  -  Server - MainScript:87
  21:41:46.542  0  -  Server - MainScript:88
  21:41:46.542  died  -  Server - MainScript:85
  21:41:46.542  Status Changed: {}  -  Server - MainScript:87
  21:41:46.542  0  -  Server -

Is it just that the for loop is repeating my print? Should i remove it?

Edit: Yes, it turns out i was just overeacting over a loop, sorry, Thank you both @FarFromLittle and @seanieplays for helping. My code wasnt broken, @seanieplays will however get the solution due to helping with table.clear which helped a lot. while yours did the same thing my script did.

I hope you understand.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.