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

How to do when a player dies to be removed from a table?

Asked by 4 years ago
01if game.Workspace.GameMode.Value == "FFA" then  --FFA gamemode
02            local spawnsTable = {}
03            for i, v in pairs(chosenClone.Spawns:GetChildren()) do
04                if v.Name == "Spawn" then
05                    spawnsTable[#spawnsTable + 1] = v
06                end
07            end
08            local playersAlive = {}
09            for i, v in pairs(game.Players:GetPlayers()) do
10                if v.Character.Humanoid.Health == 0 then return end
11                v.Character:MoveTo(spawnsTable[math.random(1, #spawnsTable)].Position)
12                local sword = game.ReplicatedStorage.ClassicSword:Clone()
13                sword.Parent = v.Backpack
14                playersAlive[#playersAlive + 1] = v
15            end
View all 33 lines...

I am trying to make a sword fighting game (the script is too long so i did not include it all,just the important part). The problem is that when a player dies, it does not gt removed from the playersAlive table, and the round will not end. Can someone please fix my code or find a more efficient way to do this? Thanks.

0
table.remove(playersAlive, table.find(playersAlive, v)) Ziffixture 6913 — 4y
0
@Ziffixture, same result, but at least you answered another question i had, i think it has something to do with the event yuni_Boy1234 320 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

I would create a function that finds the player by name, and removes it based off of the current index. When you remove an object from a table like you did in line 19, it moves the other elements down, making the i index incorrect.

01function removePlayerByName(plyrnm)
02    local i = 1
03    while playersAlive[i].Name ~= plyrnm and i <= #playersAlive do
04        i += 1
05    end
06 
07    if playersAlive[i] then
08        table.remove(playersAlive, i)
09    end
10end

And:

1for i = 1, #playersAlive do
2    local plyr = playersAlive[i]
3    plyr.Character:WaitForChild("Humanoid").Died:Connect(function()
4        removePlayerByName(plyr.Name)
5    end)
6end

I think it would be better to do this though:

01function checkGameCondition(plyr)
02    if workspace.GameMode.Value == "FFA" then
03        removePlayerByName(plyr.Name)
04    end
05end
06 
07game.Players.PlayerAdded:Connect(function(plyr)
08    plyr.CharacterAdded:Connect(function(char)
09        char:WaitForChild("Humanoid").Died:Connect(function()
10            checkGameCondition(plyr)
11        end)
12    end)
13end)
0
Edited the code a bit and it worked, thanks yuni_Boy1234 320 — 4y
1
what you said about the index was correct, but you can use table.find to return the index of that player in the table. Doing this allows you to simply do table.remove(playersAlive, table.find(playersAlive, player))  R_alatch 394 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I think something like this would work, this would remove the character from the table and replace it with nil(basically doesn't exist).

1for i, v in pairs(playersAlive) do
2    v.Character.Humanoid.Died:Connect(function()
3        playersAlive[i] = nil
4    end)
5end
0
Ends the same way, nothing changes, but thanks for trying yuni_Boy1234 320 — 4y
Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
4 years ago
Edited 4 years ago

I think the main issue was with the while loop, specifically the use of return instead of continue. I made a lot of changes here and there and did my best to explain each one.

01local sword = game.ReplicatedStorage.ClassicSword
02 
03if workspace.GameMode.Value == "FFA" then
04    --Use GetChildren() to return an array of the spawns
05 
06    local spawnsTable = chosenClone.Spawns:GetChildren())
07 
08    local playersAlive = {}
09 
10    for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
11        local character = player.Character
12 
13        if not character then
14            continue
15        end
View all 56 lines...
0
already got answered but thanks alot! please dont delete this answer, as it may be important, also you made good points yuni_Boy1234 320 — 4y
0
the main point of this is that what you had was correct and there was no need to change it, you were just using return and not continue so whatever you accepted i do not suggest using as its much more than whats needed R_alatch 394 — 4y
0
just edited my answer a lot to get rid of some of the comments, and tried to simplify the issues and what i did R_alatch 394 — 4y

Answer this question