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
9 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?

hint = script.Parent
minplayers = 4


function CheckForPlayers()
if game.Players.NumPlayers < minplayers
then
hint.Text = "You need at least "..minplayers.." to start a match."
Countdown()
else
wait (1)
hint.Text = "Starting Match in: 5"
wait(1)
hint.Text = "Starting Match in: 4"
wait(1)
hint.Text = "Starting Match in: 3"
wait(1)
hint.Text = "Starting Match in: 2"
wait(1)
hint.Text = "Starting Match in: 1"
wait(1)
hint.Text = "Starting Match in: 0"
wait(2)
hint.Text = "Starting Match."
wait(1)
hint.Text = "Starting Match.."
wait (1) 
hint.Text = "Starting Match..."
wait(1.1)
hint.Text = "Starting Match."
wait(1)
hint.Text = "Starting Match.."
wait (1) 
hint.Text = "Starting Match..."
wait(2)
hint.Text = "Starting Match."
wait(1)
hint.Text = "Starting Match.."
wait(1) 
hint.Text = "Starting Match..."
wait(1.1)
hint.Text = "Starting Match."
wait(1)
hint.Text = "Starting Match.."
wait (1) 
hint.Text = "Starting Match..."
end
end 

function Countdown()
local k = 30
while k > -1 do
wait (1)
hint.Text = "Intermission: " .. k
CheckForPlayers()
k = k - 1
end
hint.Parent.Visible = true
end
1
Put the code into a code block please. M39a9am3R 3210 — 9y
0
Are you testing with atleast 5 players? YellowoTide 1992 — 9y
0
Im testing at least 4 players. At least 4 players must be in the lobby for the match to start. TrollD3 105 — 9y

1 answer

Log in to vote
3
Answered by 9 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)

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

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:

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

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:

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

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:

local inGame = false
function TestForEnoughPlayers()
    if inGame then return end --don't start the game if it's already running
    if CheckForPlayers() then
        inGame = true
        StartMatch()
        inGame = false
    end
end
game.Players.PlayerAdded:connect(TestForEnoughPlayers)
Ad

Answer this question