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

Why cant this message appear after the game found 2 players?

Asked by 6 years ago
--Variables--
local plr = game:GetService('Players')
local sg = game:GetService('StarterGui')

--Functions--

function message(text,color)
    sg:SetCore("ChatMakeSystemMessage", {
        Text = text or error("Text is nil"),
        Color = color or Color3.fromRGB(225,225,225),
        Font = Enum.Font.SourceSans,
        FontSize = Enum.FontSize.Size24,
    })
end

function lookForPlayers()
    while true do wait()
    local playerCount = script.Parent:WaitForChild("playerCounter").existingPlayers.Value
    if playerCount >= 2 then
        print("There is enough players!")
        break
    else
        print("Not enough players!")
    end
    end
end



--Main--

lookForPlayers()

wait()

message("The round is about to start!", Color3.fromRGB(0,225,0)) -- Wont write the message in the server chat
0
The player counter script works and it recognizes that there is two players in the game, but it wont write the message in the chat after enough players has joined the server TickTockTheory 106 — 6y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

Have you tried print debugging? Perhaps playerCount isn't updating properly. Somehow the loop is becoming endless.

Reguardless, you can use the GetPlayers function of Players to return a table that consists of all the current players in game. Use the # operator on this table to return the number of entries; i.e, the number of players.

And here is a cleaner way to go about waiting with the while loop:

function lookForPlayers()
    local plrs = #game.Players:GetPlayers() --Initially recieve player amount
    --Establish loop and conditions for iteration
    while plrs < 2 and wait() do
        print("Not enough players");
        plrs = #game.Players:GetPlayers() --Update val
    end
    print("There is enough players!");
end 
0
And since this is on the client *if you wanted* you could wait even faster with http://wiki.roblox.com/index.php?title=API:Class/RunService/RenderStepped and http://wiki.roblox.com/index.php?title=RBXScriptSignal#Wait Goulstem 8144 — 6y
1
You should use .PlayerAdded and .PlayerRemoving, so you check the amount less amount of times. hiimgoodpack 2009 — 6y
0
dis guy knows about optimization :3 Goulstem 8144 — 6y
Ad

Answer this question