i want that my text1 isn’t visible if there are 2 or more players on the server. I want that if there are 2 or more that then the text 2 is visible. And if there are <2 players then there should be text1 visible and text 2 not visible. But its not working i tried with local server in studio.
Here is my script:
players = game.Players:GetChildren()
local Text1 = game.Workspace.Text1.SurfaceGui.Text
local Text2 = game.Workspace.Text2.SurfaceGui.Text
Text1.BackgroundTransparency = 1
Text2.BackgroundTransparency = 1
if #players >= 2 then
Text1.Transparency = 1
game.Workspace.Text1.CanCollide = false
Text1.TextTransparency = 1
Text2.TextTransparency = 0
If the code is inside of a while loop, you need to make sure that it will always wait before ending the loop. A good practice of this is to put a wait() at the very end of the loop.
Putting a “wait” with empty brackets will give you the minimum wait time allowed by Roblox which is roughly about 0.03 seconds. This wait time is enough for not receiving a time exhaustion error.
Also I recomend using game.Players.NumPlayers instead to get the number of players online. I would even more recommend having all this things inside of a player added event rather than a while loop. To save memory and increase game performance levels.
Also want to mention that you should use the property Visible on the TextLabel objects to hide them. Visible is a Boolean value which your are inputting true or false data into it.
The error that the script raised only happens in while loops but make sure to check how many players there are every time a player joins. Use player added event for that
Im not sure if this is what you’re looking for but you should try and use what the others said above and structure the script like this.
local Players = game:GetService("Players")
local WS = game:GetService("Workspace")
local TextOneObject = WS:WaitForChild("SurfaceOne")
local TextTwoObject = WS:WaitForChild("SurfaceTwo")
local TextOne = TextOneObject.SurfaceGui.TextLabel
local TextTwo = TextTwoObject.SurfaceGui.TextLabel
function CheckPlayersInServer()
local playersInServer = Players:GetChildren()
TextOne.BackgroundTransparency = 1
TextOne.BackgroundTransparency = 1
if #playersInServer >= 2 then
TextOneObject.Transparency = 1
TextOneObject.CanCollide = false
TextOne.TextTransparency = 1
TextOne.TextTransparency = 0
else
TextOne.TextTransparency = 0
TextOne.TextTransparency = 1
TextOneObject.Transparency = 0
TextOneObject.CanCollide = true
end
end
Players.PlayerAdded:Connect(function(player)
CheckPlayersInServer()
end)
Players.PlayerRemoving:Connect(function(player)
CheckPlayersInServer()
end)
Hope this helps
EDIT: You may need to mess with the code inside the if and else statement
EDIT 2: You may also want to check that the TextOneObject and TextTwoObject are actually found. You could also add a timeout for searching for them in case they’re not in the scene. This should not be the case however as I assume you’re doing this on the server.
So for this bit you will need to change the string to be the name of your objects
local TextOneObject = WS:WaitForChild("SurfaceOne")
local TextTwoObject = WS:WaitForChild("SurfaceTwo")
And for the EDIT 2, sometimes you might not want waitforchild to go for extended periods of time. Therefore you can add a timeout in the second parameter for WaitForChild() E.g.
local object = objectToGetChildFrom:WaitForChild("ObjectToFind", timeout)
if object then
--The object was found and did not return nil
end
There is also FindFirstChild() which will return nil if it cant find the object. This is instead of waiting for it to exist.
local object = objectToGetChildFrom:FindFirstChild("ObjectToFind")
if object then
--The object was found and did not return nil
end