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