Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make it so that when a player dies, their name is written off a table?

Asked by 6 years ago

Here's my script:

_G.addremovePlayers = function()
    for _,v in pairs(game.Players:GetChildren()) do
        table.insert(_G.playersLeft,v.Character.Name)
    end
    for i = 1,#_G.playersLeft do
    print(_G.playersLeft[i])
    end
end

I can't really seem to figure out how to remove someone's name from a table when they die.

Let me be more specific if you don't understand. 3 players join a game and when the round starts, their names are put in a table. {a, b, c}. Player b dies and I want b to be taken out of the table so it would be {a, c}.

0
I bet you didn't even read it properly. GIassWindows 141 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago

if you put this in the StarterCharacterScripts, I believe it should work:

local humanoid = script.Parent:FindFirstChild("Humanoid")
humanoid.Died:Connect(function()
    table.remove(_G.playersLeft,script.Parent.Name)
end)
0
:thinking: alright I'll try it out GIassWindows 141 — 6y
0
uh oh, looks like it didn't work! GIassWindows 141 — 6y
0
does it work fine without this part? saveourwolves 2 — 6y
Ad
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
6 years ago

Hi.

I never actually experimented with this, soo.. Let's give it a go.

function participate()
    for _, Player in pairs(game.Players:GetPlayers()) do
        _G.playersLeft[Player.Name] = true
        v.Character:FindFirstChildOfClass("Humanoid").Died:connect(function()
            print(Player.Name.. ' Has died! Remaining players: '.. #_G.playersLeft)
            _G.playersLeft[Player.Name] = nil
        end)
    end
end

game:GetService('ReplicatedStorage').RemoteFunctions.addPlayers.OnServerInvoke = participate

If it errors, let me know.

0
Oh yeah, I assumed there was a remotefunction named addPlayers, also. If that's not a thing, you might want to create it. kek Fifkee 2017 — 6y
Log in to vote
0
Answered by 6 years ago

One way to do it is to iterate through each value in that table, then get the name of the player who dies, and remove it if found.

Example: (Note you have to replace and change some stuff in the code below)

humanoid.Died:Connect(function()
for i,v in pairs(_G.playersLeft) do
if v.Name == PLAYERNAME then
table.remove(_G.playersLeft, i)
end
end
end

Answer this question