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