Check text on every play, not detected?

for some reason script on serverscriptservice didnt detect every player’s playergui
ive tried putting the local playergui to top, and using localp still didnt work

local Players = game:GetService("Players")

local function checkTimer()
	for _, player in pairs(Players:GetPlayers()) do
		local playerGui = player.PlayerGui.main.dayNIghTextlb
		if playerGui.Text == "night" then
			game.Lighting.ClockTime = 19
		end
	end
end
checkTimer()

this script suppose to be change the clocktime to 20 when every playergui’s textlabel night

2 Likes

Try using :WaitForChild() to reference PlayerGui. It would look like this:

local playerGui = player:WaitForChild("PlayerGui"):WaitForChild("main"):WaitForChild("dayNIghTextlb")
2 Likes

thanks for the suggestion but still :blush:

1 Like

Try Using ipairs instead pairs

local function checkTimer()
	for _, player in ipairs(Players:GetPlayers()) do -- ipairs instead of pairs
		local playerGui = player.PlayerGui.main.dayNIghTextlb
		if playerGui.Text == "night" then
			game.Lighting.ClockTime = 19
		end
	end
end

ipairs will return the list of the player and index.
try it

thanks for the suggestion on using ipairs but still :blush:

checkTimer() is running before the players are even added. you could use a while loop to check every second. (this definitely isn’t the best solution, but it works.)

while true do
    checkTimer()
    task.wait(1)
end

you’ll also want to use Instance:FindFirstChild, since the gui might not exist when the player is immediately added.

edit: replied the the wrong thing :sob:

1 Like

thanks for the suggestion but still :sob:

is there any output in the console?

nuuh still like before idk whats wrong

May I question does the playerGui.Text get set/changed on the client(Inside a localscript)?

its change by the serverscript

Are there any errors? If not try doing print(playerGui.Text) for a logic check.
If there is an error can you post what it is for context.

gambar

gambar

There seems to be a problem with GetPlayers() being empty because the script ran before any player joined. If 16garlicbread solution didn’t works.

You can try adding a repeat task.wait() until #Players:GetPlayers() > 0 inside CheckTimer() at the start of the function and above the loop.

If checkTimer() run once, when the playerGui.Text is changed you should have a bindableevent for checkTimer() if there a change.

The code description:
-Get all Label
-Check “night”
-Executed

You only ran that once at the start of the server
At that moment none of the labels texted “night” yet
You must add an event or something to trigger the function or do some repetitive tasks so it can check every second or something.