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

Table doesn't do anything when player dies?

Asked by 4 years ago

I am trying to make a round system in my game and I want the round to end when there is only 1 person alive, however it doesn't seem to be working, what happens is I start a local server simulation with 2 players and I reset one of them, after resetting them nothing seems to change. Here is the script (It is a regular script in the Workspace) :

    playerTable = {}
    players = game:GetService("Players")
    plrs = game.Players:GetChildren()
-----------------------------------------------------------
--Removing
players.PlayerRemoving:Connect(function(player)
    for i = 1,#playerTable do

        if playerTable[i] == player then
    print(""..player.." Has been removed from table")
            table.remove(playerTable, i)

if #playerTable <= 1 then
    game.ServerStorage.Values.InGame.Value = false
    game.ServerStorage.Values.TopText.Value = "The winner is" (""..playerTable.."")""
end
        end
    end
end)

--Died
for i = 1,#playerTable do
    playerTable[i].Character.Humanoid.Died:Connect(function()
        table.remove(playerTable, i)

if #playerTable <= 1 then
    game.ServerStorage.Values.InGame.Value = false
    game.ServerStorage.Values.TopText.Value = "The winner is" (""..playerTable.."")""
end
    end)
end

The script doesn't seem to do anything when I reset a player as a test

1 answer

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The script will run the for loop when the game starts, meaning just before the player loads into the game. Instead of creating the died event there, make the event when the player joins the game.

Edit:

Players = game:GetService("Players")

function characterDied()
    --do stuff here
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)

        --create died function for the player
        character.Humanoid.Died:Connect(function()
            characterDied()
        end)

    end)
end)

0
How would I change it so it only activates when the player joins the game? BriskyDev 4 — 4y
0
check my edit royaltoe 5144 — 4y
0
did this work? please mark it as accepted if it works. royaltoe 5144 — 4y
Ad

Answer this question