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

Why Isn't my Script working?

Asked by 9 years ago

Why isn't this working? Its spawning the hint box at the top and it stays black and its not saying "You need at least 2 players to begin a round" , how do i make this work?

local h = Instance.new("Hint",workspace)
local minPlyrs = 2

function CheckForPlayers()
    if game.Players.NumPlayers < minPlyrs then
        h.Text = "You Need Atleast "..minPlyrs.." To Begin A Round!"
        Countdown()
    else
        h.Text = " Minimum Player Count Reached! Beggining Round!"


    end
end

function Countdown()
    for i = 10,1, -1 do
        wait(1)
        h.Text = i.."Second Before Round Starts"
    end
    CheckForPlayers()
end

wait(1)
0
line 09 else "if game.Players.NumPlayers>minPlyrs then" --Code here woodengop 1134 — 9y
0
You need to trigger the CheckForPlayers function somehow, through a PlayerAdded event most likely. M39a9am3R 3210 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

You've defined functions, but you aren't calling them (or hooking them up to events). You need to do something like game.Players.PlayerAdded:connect(CheckForPlayers).

Also, you're using infinite recursion. Although it's extremely improbable that there would be precisely one player in one of your servers for long enough that the current code would error, if you expand on the script, it might become more likely. If you have functions call each other and they never return from the function, this means you are adding more and more functions to the stack. Roblox allows ~16000 functions on the stack before erroring.

There are two ways you can address the issue:

  1. Use a while loop that controls the flow of the game

  2. Use event based programming

ex of #1:

local h = Instance.new("Hint",workspace)
local minPlyrs = 2

function CheckForPlayers() --returns true if enough players
    return game.Players.NumPlayers >= minPlyrs
end

function Countdown()
    for i = 10,1, -1 do
        h.Text = i.."Second Before Round Starts"
        wait(1)
    end
end

while true do
    if CheckForPlayers() then
        h.Text = " Minimum Player Count Reached! Beggining Round!"
        wait(1)
        Countdown()
        StartGame() --you have to write this function
    else
        h.Text = "You Need Atleast "..minPlyrs.." To Begin A Round!"
        wait(1) --wait 1 second before repeating the loop
    end
end

Notice that I took the "h.Text = " lines out of CheckForPlayers and moved them into the while loop. This allows you to call CheckForPlayers from anywhere without worrying about it messing with the hint.

You can make the loop more complicated as desired. It's a bit flawed: a player might leave the game after CheckForPlayers() returns true, but before StartGame is called, for instance. You can improve the functions and the loop to account for these issues (ex you might have the Countdown function call CheckForPlayers every second and return early if there are too few players left)

The second method (using events) works better if the game does not have a defined length (ie if you don't know if the game will last 5 minutes or 10). You might use game.Players.PlayerAdded to see if you have enough players to start the game (if the game isn't already started). You could use game.Players.PlayerRemoving to consider ending the game (if the game is started). You'd need to create an event or loop to check for the end of the game. ex, say the game ends when someone hits a certain brick, you could then connect the Touched event of that brick to a "GameOver" function.

A final note: "Hint"s are deprecated. You should use GUIs to transmit the message to each player.

0
Thanks! QuantumScripter 48 — 9y
Ad

Answer this question