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

I NEED HELP [Solved] [closed]

Asked by 10 years ago

Here's my script:

playerCount = {}
Players = Game:GetService("Players")

Players.PlayerAdded:connect(function(Player)
    table.insert(playerCount, Player.Name)
    print(table.concat(playerCount, ", "))
end)

Players.PlayerRemoving:connect(function(Player)
    for index = 1, #playerCount do
        if playerCount[index] == Player.Name then
            table.remove(playerCount, index)
            print(table.concat(playerCount, ", "))
            break
        end
    end
end)

epic = true
while epic == true do
    if #playerCount == 2 then
    local hint = Instance.new("Hint", Workspace)
    hint.Text = "The game will begin shortly."
    epic = false
    else hint = "Two people are needed to start the game."
    end
end

The only thing I have a problem with is that last part.

What it's supposed to do is: until 2 players have entered, it's gonna have a hint at the top of the screen saying "Two players are needed to start the game." until 2 players have entered. Once 2 players have entered, it will show a hint saying, "The game will begin shortly."

Only, no hint is showing up whenever I do it.

Please help me with this.

Locked by TheMyrco

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
4
Answered by
jobro13 980 Moderation Voter
10 years ago

Yeah. Some general tips on this:

  • Your approach on getting the number of players is unneeded. You can use #game.Players:GetPlayers() for this.
  • Your script has a logic bug.

In the case that there are not two players - a non-waited while loop runs. This should crash your script. In the case there are more or two players - epic is set to false and the loop will break, never checking this again. You want to set the hint text, not the hint itself (line 25). (So, this won't actually create or set a hint)

Let me provide a fix:

function GetNumplayers()
return #game.Players:GetPlayers()
end

local Hint = Instance.new("Hint", game.Workspace)

while wait(1) do
    if GetNumplayers() >= 2 then
        Hint.Text = "The game will begin shortly"
    else
        Hint.Text = "Two people are needed to play this game"
    end
end
1
Wait, so really, I didn't need all that about adding the players into and out of playerCount, I could of just done this instead??? ImmenseKassing 120 — 10y
1
Yes. But believe me, this happens to everyone. I once rewrote math.atan2 - after 2 minutes I noticed it. "Hmm, what is math.atan2? Aha! The function I just wrote ._.". These are just things you have to know. Your approach was good though, that worked! The while loop was the problem here. jobro13 980 — 10y
1
This actually took me hours of asking for help and trying to figure stuff out in order to get this. Thanks! ImmenseKassing 120 — 10y
1
That's even better. What you did was not wrong - I'm just telling here that you could also have shortened your code. You now at least know how to use events, the "table" library and how to get table lengths. It has not be a waste! jobro13 980 — 10y
Ad