How to check if player is alive in a global script?

I am making minigames in studio and I’m trying to make functions that when they run it checks if the player is alive(which i dont know how to check if they are alive) so that it can teleport the player into the map which if they are alive and when they teleport it adds to the value (PlayersAliveInMinigame) by 1 so that the game knows how many people are in the minigame. the second function is one that is called when the player dies or when someone disconnects from the server and in these cases if the player died then humanoid.died event gets called and calls the function which the function then checks if they are alive (which i dont know how to check if they are alive (in a global script)) and if they are dead then it sets their InMiniGame bool value to false because that means they died so they arent in the minigame anymore and now they go back to the lobby and also it subtracts the variable (PlayersAliveInMinigame) by 1 because they arent alive in the minigame anymore and when that value hits 1 then the game ends since there is only one player left alive and if they disconnect then Player.PlayerRemoving is fired and if they are in the minigame it subtracts the playeraliveinminigame value since they arent in the game anymore while being in the minigame.

I could execute all of this if i knew how to check each invidividual players humanoid health value in a globalscript like if its less than or equal to 0 then that means they are dead but if its greater than 0 then that means they are alive and i have no idea how to check this and i dont know if a remoteevent would work and im not so good with them either.

is there something i can do to check if they are alive with humanoid.died? i cant really think of it and i dont know.

Please help me if you have an idea of how to do this :frowning:

2 Likes

Would using this code work for your case?

local Players = {}
for i,v in pairs(game.Players:GetPlayers()) do -- Get all players and loop through them
	-- This checks a few things:
	-- The player has a character (otherwise they are dead)
	-- The character isn't being destroyed or parent is set to nil
	-- The character has a humanoid
	-- The humanoid's health is above 0
	-- If the player passes all those checks it is alive
	if v.Character and v.Character.Parent ~= nil and v.Character:FindFirstChild("Humanoid") and v.Character.Humanoid.Health >= 0 then
		Players[v.Name] = true -- Means the player is alive
	else
		Players[v.Name] = false -- Means the player is dead
	end
end

for k,v in pairs(Players) do
	print(k, v and "alive" or "dead") -- this is just a simple one line if statement
end

-- To check if a player is alive, just do this:
-- Players[Player.Name]
-- If true the player is alive, otherwise they are dead.

Make sure to run the loop every time you update something (such as before teleporting, adding points, etc) so you have the most up to date information.
I would make this a function if it needs to be run more than once in a script.
If this method doesn’t work well (for example you are checking it often; and if you are doing that, an event based system is often better.), you can use Player.CharacterAdded and Humanoid.Died on the humanoid when the character is added, and change the value of whatever value you use to check the player is alive.
Useful Documentation:
PlayerAdded
CharacterAdded

7 Likes

Something basic like

for i,v in pairs(game.Players:GetChildren()) do
    if v.Character then
        if v.Character.Humanoid.Health > 0 then
            --add them to the table
            --increase number of players alive + 1
        end
    end 
end

This would go through every player, see if they have a character, and then check if they are alive. Pretty basic loop.

3 Likes

I have the same issue. I am using this script to check when the player dies every second of the round in a server script. How would I keep track of who died until the end of the round?

I think I’ve found a similar question to this before: How to remove a player from a table when he dies. In this one, it attaches a function to when a player dies. This could easily be modified a bit to work like you would like it to.
In your case it seems like something along these lines should work.

local players = {} 
game.Players.PlayerRemoving:Connect(function(player)
	if table.find(players,player.UserId) then
		table.remove(players,table.find(players,player.UserId))
	end
end)
while true do
	for _,player in pairs(game.Players:GetPlayers()) do
		table.insert(players,player.UserId)
		continue
	end

	local connection
	for _, playerId in pairs(players) do
		local playerModel = game.Players:GetPlayerByUserId() do
			if playerModel then
				connection = playerModel.Character:WaitForChild("Humanoid").Died:Connect(function() -- runs the function when player has died
					print(playerModel.Name .. " has died!")
					connection:Disconnect() -- This part makes it so if the same player dies again the script wont run. If you want to make it so the script keeps running even though the player has died once then remove this part
					table.remove(players, table.find(players,playerId)) --Checks for player spot in "players" table then removes them from it
					--If you would like, you *can* set the playerModel's InMiniGame bool here, but only if it is needed outside the scope of this server script
				end)
			end
		end
	end
	repeat
		wait()
		--Do stuff here
	until #players <= 1
	print("Round ends!")
	wait(10)
	table.clear(players) --Cleared to make sure that no duplicates end up in the future round; If you dont want to clear it, you may just add a simple "if not table.find(players,player.UserId) then" check right before it adds the players to the players table.
end

Hopefully this helps(all credit should go to elomala, the person who solved the other devforum post).