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

Why will the player not get removed from a table upon death?

I don't know how clear I have to because of the title. But I am trying to do is when a player dies they are removed from a table. I did in fact get an error which was:

ServerScriptService.MainScript:68: invalid argument #2 to 'remove' (number expected, got Instance)

Here's my code.



--// Variables
local status = game.ReplicatedStorage:WaitForChild("Status")
local plrsLeft = game.ReplicatedStorage:WaitForChild("Players")
local timer = game.ReplicatedStorage:WaitForChild("Timer")
local platesWeClone = game.ServerStorage:WaitForChild("Plates")
local income = 5
local gameTime = 120
local intermissionTime = 10
--// Functions
local function SetTimer(op)
	local minutes = math.floor(op/60)
    local seconds = math.floor(op%60)
    timer.Value = (string.format("%.2i:%.2i", minutes, seconds))
end
local function SetStatus(op)
	status.Value = tostring(op)
end
local function SetPlrs(NumPlrsInTable)
	plrsLeft.Value = tostring(NumPlrsInTable)
end

--// Loop
while true do
	for i = intermissionTime,0,-1 do
		SetTimer(i)
		SetStatus("Intermission.")
		SetPlrs(0)
		local plrs = {}
		wait(1)
		if i == 0 then
			for i,v in pairs(game.Players:GetPlayers()) do
				if v then
					table.insert(plrs,v)
				end
			end
			SetStatus("Cleaning play area.")
			wait(0.5)
			game.Workspace.Plates:Destroy()
			SetStatus("Play area cleaned, game starting soon!")
			local newPlates = platesWeClone:Clone()
			newPlates.Parent = workspace
			for e = 5,0,-1 do
				SetTimer(e)
				wait(1)
				if e == 0 then
					SetStatus("Teleporting players to play area, do not move.")
					for i,v in pairs(game.Players:GetPlayers()) do
						if v then
							local character = v.Character
							local spawns = game.Workspace:FindFirstChild("SpawnPoints")
							local AvailableSpawns = spawns:GetChildren()
							if character:FindFirstChild("HumanoidRootPart") then
								character:FindFirstChild("HumanoidRootPart").Anchored = true
								character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawns[1].CFrame + Vector3.new(0,5,0)
								table.remove(AvailableSpawns,1)
							end
							wait(5)
							character:FindFirstChild("HumanoidRootPart").Anchored = false
							for o = gameTime,0,-1 do
								SetTimer(o)
								SetStatus("Game in progress.")
								SetPlrs(#plrs)
								wait(1)
								for index, value in ipairs(plrs) do
							    if value == v.Name then -- trying to find the player name in the table
							        print(value .. " found at index " .. index) -- index will help remove the error because it was expecting a number, not an instance
							
										 for i, v in pairs(game.Players:GetPlayers()) do
											 if v then
												local character = v.Character
												character.Humanoid.Died:Connect(function()
												table.remove(plrs, index)
												 end)
											end
										end
									end
								end

								if o == 0 then
									table.getn(plrs).Data.Money.Value = table.getn(plrs).Data.Money.Value + income
									v:LoadCharacter()
								end
							end
						end
					end
				end
			end
		end
	end
end


2 comments

0

edited my answer.

0

Still not working.

1 answer

Another way of doing this, we could detect if the player is inside of the table, and then detecting where the player name is in the table, and removing it. If you don't know much about pairs, here is an example:

local players = {"Ali", "Ben", "Cammy"}
 
for playerIndex, playerName in ipairs(players) do
    print(playerIndex .. " is " .. playerName)
end

Output ^^

1 is Ali, 2 is Ben, 3 is Cammy,

Here's the code that will possibly solve your question.

local plrs = {}
for index, value in ipairs(plrs) do
    if value == v.Name then -- trying to find the player name in the table
        print(value .. " found at index " .. index) -- index will help remove the error because it was expecting a number, not an instance

        for i, v in pairs(game.Players:GetPlayers()) do
            if v then
                local character = v.Character
                character.Humanoid.Died:Connect(
                    function()
                        table.remove(plrs, index)
                    end
                )
            end
        end
    end
end

If this does not work, you can try:

for i, v in pairs(game.Players:GetPlayers()) do
    if v then
        local character = v.Character
        character.Humanoid.Died:Connect(
            function()
                for i = 1, #plrs do
                    if plrs[i] == v.Name then
                        table.remove(plrs, i)
                    end
                end
            end
        )
    end
end

More information: https://education.roblox.com/en-us/resources/pairs-and-ipairs-intro

If it works, please accept the answer, so we will both get some reputation.

4 comments

0

Doesn't work.

0

Do you want me to put the entire script in?

0

Sure.

0

Okay, it's up.

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