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

My NumPlayer checking isn't working?

Asked by
Relatch 550 Moderation Voter
9 years ago

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
0
What is the error? DigitalVeer 1473 — 9y
0
I can only post and answer every 1.5 answers... I'm waiting... I know the error... I think. EzraNehemiah_TF2 3552 — 9y
0
Replication: Can't create default object of type Workspace Relatch 550 — 9y
0
Please tell us the whole error word for word please! EzraNehemiah_TF2 3552 — 9y
View all comments (8 more)
0
That is just an error based on ROBLOX's part. It does not effect your code in any manner DigitalVeer 1473 — 9y
0
it doesn't work though Relatch 550 — 9y
0
I did do the full error. Relatch 550 — 9y
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 DigitalVeer 1473 — 9y
0
I put a print before the check for numplayers, it didnt print out. Relatch 550 — 9y
0
Check the hierarchy. EzraNehemiah_TF2 3552 — 9y
0
I went into the game and pressed "F9" and nothing came up. Relatch 550 — 9y
0
The script works. Make sure your gui is visible on the screen and that it is within a ScreenGui object. FearMeIAmLag 1161 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

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!)

0
No error, and didn't do anything to the gui text. Relatch 550 — 9y
Ad

Answer this question