Changing Text if there are 2 players on the server?

Hi,

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

else

Text1.TextTransparency = 0
Text2.TextTransparency = 1
Text1.Transparency = 0
game.Workspace.Text1.CanCollide = true

end

Error:

[16:02:14.383 - Script timeout: exhausted allowed execution time
16:02:14.385 - Stack Begin

Thanks!

1 Like

Did you put this in a while loop?

1 Like

Is this in a while true do loop? If so you need a wait.

no it isnt thats the full script

maybe its a error from a other script idk

Hey! I hope this will help:

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.

hi thanks, but the script isnt in a while loop

From what line is the error? It should show where the error was if you click on it.

i think its because its only checking once

error is fixed but the text isnt working

how i can fix that? im new at scripting

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

k i will try. But the error is fixed dont think about the error :smiley: it was a other script

1 Like

you can just use the playeradded event and add to a value everytime playeradded functions and use an if statement after it

i tried but it dont work

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

local Players2 = game:GetService(“Players”)

Players2.PlayerAdded:Connect(function(player)

if #players >= 2 then
	Text1.Transparency = 1
	game.Workspace.Text1.CanCollide = false
	Text1.Visible = false
	Text2.Visible = true

else

	Text1.Visible = true
	Text2.Visible = false
	Text1.Transparency = 0
	game.Workspace.Text1.CanCollide = true
end

if game.ReplicatedStorage.roundover == true then
	--coming soon
else
	-- coming soon
end
end)

can someone send the full script :smiley:

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 :slightly_smiling_face:

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.

its not working :confused: i pasted the script

wdym with edit 2. Im not a native speaker :smiley:

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