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

Dead/Alive function problems?

About a week ago, I was trying to make a function that would make it so that if all players are dead or if 1 player is alive it would stop the round, so here is what a few people and I came up with:

[PLAYERDEAD/ALIVE FUNCTION]

local wordValue = game.ReplicatedStorage.valueStorage.wordValue
local timeValue = game.ReplicatedStorage.valueStorage.timeValue

function playersCurrentlyPlaying()  
    local playerDead = 0

    for loopTime, loopPlayers in ipairs (game.Players:GetChildren()) do
        if loopPlayers.playValue.Value == 0 then 
           playerDead = 1
        end
    end

    if playerDead == 0 then 
       print 'Players are alive!'
    elseif playerDead == 1 then
       wordValue.Value = "All players have died!"
       timeValue.Value = "0:00"
    end
end

[FUNCTION IT'S USED IN]

function roundCountdown()
	wordValue.Value = 'Time left!'
	
    for minuteTime = 2, 0, -1 do
        for secondsTime = 59, 0, -1 do
            timeValue.Value = string.format("%d:%.2d", minuteTime, secondsTime)
			playersCurrentlyPlaying()
            wait(1)
        end
    end
end

It doesn't seem to work properly and I was wondering if someone could help me fix it so that if all players are dead or if 1 player is alive, it would stop the round?

I'm not an experienced scripter, so please let me know how to fix it with a decent explanation, thanks!

2 comments

0

Have you asked this question multiple times? I believe I've seen the name of the question at least for the third time already.

0

Yes, but there hasn't been an answer. This will be the last time though, sorry!

1 answer

✓ Accepted answer

The best way I can think to do this is decrement a variable, lets call it playersRemaining, everytime a player is eliminated from the game (in this case, because of a death).

The first thing you need to do is detect when a player is eliminated. You can do this in a variety of ways, but in this case I'll just stick a Died event listener onto every players humanoid (you can certainly modify this, say to allow people to sit out of rounds for example).

The second thing you'll need is your timer loop, which you've already got a decent start on.

local timeValue = game.ReplicatedStorage.valueStorage.wordValue
local wordValue = game.ReplicatedStorage.valueStorage.timeValue

local roundLength = 180
local playersRemaining = 0

local function updateValues(timeLeft, message)
	local minutes = math.floor(timeLeft/60)
	local seconds = math.floor(timeLeft%60)
	timeValue.Value = string.format("%d:%.2d", minutes, seconds)
	wordValue.Value = message
end

local function roundTimer()
	local timeLeft= roundLength
	
	while playersRemaining > 0 do
		local elapsed, _ = wait()
		timeLeft= timeLeft - elapsed
		updateValues(timeLeft, "Time Left!")
	end
end

local function startRound()
	playersRemianing = game.Players.NumPlayers

	for _, player in next, game.Players:GetPlayers() do
		player.Character.Humanoid.Died:connect(function()
			playersRemaining = playersRemaining - 1
		end)
	end

	roundTimer()
	
	if playersRemaining > 0 then
		updateValues(0, "Round Over!")
	else
		updateValues(timeLeft, "All Players Died!")
	end
end

There are certainly many ways you can implement and reorganize this, but this gives a decent foundation to build onto.

2 comments

0

Thank you so much, this helps a lot!

0

No problem :)

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