01 | if 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 |
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.
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.
01 | function 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 |
10 | end |
And:
1 | for i = 1 , #playersAlive do |
2 | local plyr = playersAlive [ i ] |
3 | plyr.Character:WaitForChild( "Humanoid" ).Died:Connect( function () |
4 | removePlayerByName(plyr.Name) |
5 | end ) |
6 | end |
I think it would be better to do this though:
01 | function checkGameCondition(plyr) |
02 | if workspace.GameMode.Value = = "FFA" then |
03 | removePlayerByName(plyr.Name) |
04 | end |
05 | end |
06 |
07 | game.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 ) |
13 | end ) |
I think something like this would work, this would remove the character from the table and replace it with nil(basically doesn't exist).
1 | for i, v in pairs (playersAlive) do |
2 | v.Character.Humanoid.Died:Connect( function () |
3 | playersAlive [ i ] = nil |
4 | end ) |
5 | end |
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.
01 | local sword = game.ReplicatedStorage.ClassicSword |
02 |
03 | if 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 |