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

Table doesn't do anything when player dies?

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

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)

3 comments

0

How would I change it so it only activates when the player joins the game?

0

check my edit

0

did this work? please mark it as accepted if it works.

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