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

Why is it not working?

Asked by
TrollD3 105
10 years ago

Im trying to make an intermission thing for my fps lobby but its not working. Should I put this in a regular script or local script? Also can someone ell me how to fix this?

01hint = script.Parent
02minplayers = 4
03 
04 
05function CheckForPlayers()
06if game.Players.NumPlayers < minplayers
07then
08hint.Text = "You need at least "..minplayers.." to start a match."
09Countdown()
10else
11wait (1)
12hint.Text = "Starting Match in: 5"
13wait(1)
14hint.Text = "Starting Match in: 4"
15wait(1)
View all 59 lines...
1
Put the code into a code block please. M39a9am3R 3210 — 10y
0
Are you testing with atleast 5 players? YellowoTide 1992 — 10y
0
Im testing at least 4 players. At least 4 players must be in the lobby for the match to start. TrollD3 105 — 10y

1 answer

Log in to vote
3
Answered by 10 years ago

This code should be in a non-local script; it's part of the code that controls gameplay and thus affects more than one player.

You haven't explained exactly what you want versus what you're getting.

I know this isn't your question, but I recommend using a for loop instead of repeating the same lines over and over again, specifically in the CheckForPlayers() function. Though it makes it harder to have minor modifications to a specific iteration (ex you have a couple "wait(2)" and "wait(1.1)" for some reason), it's otherwise a better idea (less code; easier to change once you get used to it)

1for timeLeft = 5, 0, -1 do
2hint.Text = "Starting Match in: " .. timeLeft
3wait(1)
4end

Otherwise, I think your problem stems from the fact that your CheckForPlayer function does two different things, so your logic in the Countdown function becomes confused. To fix this, do the following:

Have CheckForPlayer only check the number of players. It should return a boolean (true/false) depending on what it finds:

1function CheckForPlayers()
2    return game.Players.NumPlayers >= minplayers
3end

With what I wrote, the function will return true if there are enough players; false otherwise. Put the code that starts a new match in a function by itself, and call that when CheckForPlayers returns true.

Next, your Countdown function needs to do different things based on the result from CheckForPlayers. I'm not entirely sure what you want to happen, but let's say you want the intermission to last 30 seconds, and then you want to keep checking for players until there are enough. In that case:

1function Countdown()
2    hint.Parent.Visible = true
3    for k = 30, 0, -1 do
4        hint.Text = "Intermission: " .. k
5    end
6    while not CheckForPlayers() do wait(1) end --Check for players every second. Once there are enough, we continue beyond this line.
7    StartMatch() --let this be the function where you have the "Starting Match in:" code
8end

Also, be wary of using functions that endlessly call each other. Roblox can handle over 16000 recursions (functions calling each other without ever returning), but after that will error. Though it's unlikely a server will last that long (especially since your functions wait for long periods of time before calling the next one), it's still better practice to avoid this behaviour altogether.

The alternative is to use events to detect when players have joined/left your place, when they've died, etc, and avoid loops that are continually checking for a condition to change. For instance, instead of while not CheckForPlayers() do wait(1) end, it would be better to do this:

01local inGame = false
02function TestForEnoughPlayers()
03    if inGame then return end --don't start the game if it's already running
04    if CheckForPlayers() then
05        inGame = true
06        StartMatch()
07        inGame = false
08    end
09end
10game.Players.PlayerAdded:connect(TestForEnoughPlayers)
Ad

Answer this question