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

How to remove a value from a table?

So if I have a block of code that creates active contestants:

ActiveContestants = {}
		for _, player in pairs(Contestants) do
			if player then 
				local Character = player.Character
				if Character then 
					local MatchTag = Character:FindFirstChild("MatchTag")
					local Humanoid = Character:FindFirstChild("Humanoid")
					if MatchTag and Humanoid and Humanoid.Health > 0 then
						table.insert(ActiveContestants, player)
					end
				end
			end
		end
		end

How would I make something that removes the player from the table on the death of the player

function NewCharacter(KilledPlayer, Character)
	local Humanoid = Character:FindFirstChild("Humanoid")
	if Humanoid then 
		Humanoid.Died:connect(function()
-- Would I put something here like:
table.remove(ActiveContestants, KillerPlayer)
		end)
	end
end

I pretty much want the player to be removed from the table when they die or if the matchtag is no longer in their character. I'm not sure how to go about this but a problem I am thinking will happen is if someone resets without a matchtag then it will error.

1 comment

0

If you want to use table.remove, then you need to have the place index as the second parameter

2 answers

It is best if you use a key value pair so we do not need to find the index to clear the players name, we can just use the players name and then clear the key ect.

Here is a quick example:-

-- get the service
local plrSrv = game:GetService('Players')

-- the list of active players
local activeContestants = {}

plrSrv.PlayerAdded:connect(function (plr)
	-- add the tag and events when the character spawns
	plr.CharacterAdded:connect(function (charModel)
		Instance.new('Part', charModel).Name = 'Tag'
		
		-- clear the player from the list when they die
		local hum = charModel:WaitForChild('Humanoid')		
		hum.Died:connect(function()
			activeContestants[plr.Name] = nil
		end)		
		
		-- add an event to detect when the tag is removed from the characters model
		charModel.ChildRemoved:connect(function (inst)
			if inst.Name == 'Tag' then
				-- remove from the list
				activeContestants[plr.Name] = nil -- clear the key
				-- could also do other things
			end
		end)
	end)
end)

-- clear the player from the list when the leave the game if needed
plrSrv.PlayerRemoving:connect(function (plr)
	activeContestants[plr.Name] = nil -- clear the key
end)

-- just a test rig
while wait(10) do
	print('Players active')
	for i, _ in pairs(activeContestants) do
		print(i)
	end
	
	wait(2)
	print('Makeing all players active')
	for _, v in pairs(plrSrv:GetChildren()) do
		if v.Character then
			-- reset for the tag
			if not v.Character:FindFirstChild('Tag') then
				Instance.new('Part', v.Character).Name = 'Tag'
			end
		end
		activeContestants[v.Name] = true
	end
	print('All players are now active')
end

I have used the ChildRemoved so we can tell when the tag is removed from the player.

I hope this helps, please commend if you do not understand how / why this code works.

It is often cleaner and easier to compute the list based on the state of the world, rather than manually adding/removing from ActiveContestants.

i.e., if you just make ActiveContestants a function:

function ActiveContestants()
	local out = {}
	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Character then
			if player.Character:FindFirstChild("MatchTag")
			    and player.Character.Humanoid.Health > 0 then
				table.insert(out, player)
			end
		end
	end
	return out
end

and call the function whenever you need who is still playing. No worries about adding/removing at all -- it just is the correct value whenever you call it.

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