Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My NumPlayer checking isn't working?

I keep getting an error in my script, and I don't know why. Help?

local player = game.Players.LocalPlayer
local messagegui = player.PlayerGui:FindFirstChild("Messages")
local messageline = messagegui:FindFirstChild("MessageLine")

repeat wait() until player and messageline and messagegui

while wait() do
    if game.Players.NumPlayers >= 2 then
        messageline.Text = "Game about to start."
    else
        messageline.Text = "Waiting for 2 or more players."
		break
    end
end

12 comments

0

What is the error?

0

I can only post and answer every 1.5 answers... I'm waiting... I know the error... I think.

0

Replication: Can't create default object of type Workspace

0

Please tell us the whole error word for word please!

0

That is just an error based on ROBLOX's part. It does not effect your code in any manner

0

it doesn't work though

0

I did do the full error.

0

Your code is rather wrong somehow. Are you sure that these 'messages' and 'messageline' instances exist? Also, put some prints to make sure that certain scopes are being run

0

I put a print before the check for numplayers, it didnt print out.

0

Check the hierarchy.

0

I went into the game and pressed "F9" and nothing came up.

0

The script works. Make sure your gui is visible on the screen and that it is within a ScreenGui object.

1 answer

✓ Accepted answer

I'm guessing that well, LocalScripts run before the player is actually created. So you need to add this near the start of your script at about line 4:

repeat wait() until player and messageline and messagegui

Also, instead of doing WaitForChild for messagegui, do FindFirstChild. Because what I think is, your trying to find MessageLine in something that doesn't exist, like saying, "Get my phone in my backpack" when you don't have a backpack with you.

local player = game.Players.LocalPlayer
local messagegui = player.PlayerGui:FindFirstChild("Messages")
local messageline = messagegui:FindFirstChild("MessageLine")

repeat wait() until player and messageline and messagegui

while wait() do
    if game.Players.NumPlayers < 2 then
        messageline.Text = "Game about to start."
    else
        messageline.Text = "Waiting for 2 or more players."
    end
end

Since you have an infinite loop, you might want a break or use ChildAdded for the players service:

local player = game.Players.LocalPlayer
local messagegui = player.PlayerGui:FindFirstChild("Messages")
local messageline = messagegui:FindFirstChild("MessageLine")

repeat wait() until player and messageline and messagegui

while wait() do
    if game.Players.NumPlayers < 2 then
        messageline.Text = "Game about to start."
    else
        messageline.Text = "Waiting for 2 or more players."
	break
    end
end

or:

local player = game.Players.LocalPlayer
local messagegui = player.PlayerGui:FindFirstChild("Messages")
local messageline = messagegui:FindFirstChild("MessageLine")

repeat wait() until player and messageline and messagegui

game:GetService("Players").ChildAdded:connect(function(obj)
    if game.Players.NumPlayers < 2 and obj:IsA("Player") then
        messageline.Text = "Game about to start."
    else
        messageline.Text = "Waiting for 2 or more players."
    end
end)

If you want to start the game when there is 2 OR MORE players, instead of doing, NumPlayers < 2 switch the sign around, since how you set it up it says, "If there is less than 2 players" and add a = to the end so it says "If there is 2 or more players then..."

local player = game.Players.LocalPlayer
local messagegui = player.PlayerGui:FindFirstChild("Messages")
local messageline = messagegui:FindFirstChild("MessageLine")

repeat wait() until player and messageline and messagegui

game:GetService("Players").ChildAdded:connect(function(obj)
    if game.Players.NumPlayers >= 2 and obj:IsA("Player") then
        messageline.Text = "Game about to start."
    else
        messageline.Text = "Waiting for 2 or more players."
    end
end)


Hope this helps!(I don't know your error since at the time I am typing this you haven't told me the error yet, so tell me if there is still an error and I'll fix it!)

1 comment

0

No error, and didn't do anything to the gui text.

This discussion is preserved as it was. New answers, comments, and votes are closed.