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

Is there a fix?

So I know I've asked a ton of questions on this but even though I have been tinkering around a lot with this script I need someone who knows what they're doing better than I do. Using this script I would like it to display a message when either the time runs out or they're are no "infected" players.

function survivor_finish(text,time)
    time_left = 20--just to test with
        while time_left >= 0 do
            time_left=time_left-1
            wait(1)
            print(time_left)
    if #getTeam(Game.Teams.Infected) == 0 or time_left==0 then
        m=Instance.new("Message",Workspace)
                m.Text = text
                wait(time)
                m:Destroy()
    end
    end
    end
 
survivor_finish("Time has run out, survivors win!",3)

Thanks!

1 answer

Please tab your code properly.


Since the if for ending the game doesn't actually stop the loop, the loop will just keep going if all Infected are gone.

We need to add a return or a break to make it stop.

I suggest returning the team that won:

function waitForWinner(text,time)
	time_left = 20--just to test with
	while time_left >= 0 do
		time_left = time_left - 1
		wait(1)
		print(time_left)
		if #getTeam(game.Teams.Infected) == 0  then
			return game.Teams.Survivors;
		end
	end
	return game.Teams.Infected;
end

local winner = waitForWinner();

I am assuming Survivors is the name of the team that opposes Infected (and that there is an opposing team) and that Infected wins if the time runs out.

I would suggest moving the message creation to deal with winner rather than to actually be done inside this function. It should keep things cleaner and easier to understand.

Those might not be correct assumptions but it should be easy to tweak this to fit your game!

Fixed typo in condition; time_left == 0 shouldn't have been a check for the other team winning

6 comments

0

Can you describe what this does maybe?

1

There are 20 seconds per round; if all the Infected die, we say "survivors" wins; otherwise "infected" win. Though I just realized I had a mistake in the condition on line 7, sorry about that, it's fixed now

0

Can I make it so it is checking for a winner during the while loop?

1

Of course, that is what this is doing currently, and you could also add additional conditions.

0

It seems to wait for the time to be over for the message to be displayed. I'll check again.

0

Yeah it waits for the time to end then it displays the message

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