Only one TV is changing text and other's not

Hello, so here is the problem. I’m trying to make an schedule in game where an TV shows, however, only one of them is changing and others not.

image - Location
image - Problem

-ServerScript - ServerScriptService

local SCW = game.Workspace.Map.CCI.SCW
local Folder = SCW.Script
local Sound = Folder.CheckUp.SoundBlock.Camp

local TVs = {}
for i, v in pairs(Folder:GetDescendants()) do
	if v:IsA('TextLabel') and string.find(v.Name, "Time") then
		table.insert(TVs, v)
	end
end

while true do
	for i, v in ipairs(TVs) do
		v.Text = "RECOLHIMENTO"
		Sound:Play()
		wait(7)
		v.Text = "TEMPO LIVRE"
		Sound:Play()
		wait(7)
		v.Text = "REFEIÇÃO"
		Sound:Play()
		wait(7)
		v.Text = "CHECKUP"
		Sound:Play()
		wait(7)
		v.Text = "TEMPO LIVRE"
		Sound:Play()
		wait(7)
		v.Text = "CHECKUP"
		Sound:Play()
		wait(7)
		v.Text = "PRÉ-LOCKUP"
		Sound:Play()
		wait(7)
		v.Text = "LOCKUP"
		Sound:Play()
		wait(7)
	end
end
1 Like

To change every TV at the same time, you can utilize the task.spawn function and place the code you want to run in the for loop inside it. This way, the for loop will not be yielded by the waits.
The for loop should look like this:

for i, v in ipairs(TVs) do
	task.spawn(function()
		v.Text = "RECOLHIMENTO"
		Sound:Play()
		task.wait(7)
		v.Text = "TEMPO LIVRE"
		Sound:Play()
		task.wait(7)
		v.Text = "REFEIÇÃO"
		Sound:Play()
		task.wait(7)
		v.Text = "CHECKUP"
		Sound:Play()
		task.wait(7)
		v.Text = "TEMPO LIVRE"
		Sound:Play()
		task.wait(7)
		v.Text = "CHECKUP"
		Sound:Play()
		task.wait(7)
		v.Text = "PRÉ-LOCKUP"
		Sound:Play()
		task.wait(7)
		v.Text = "LOCKUP"
		Sound:Play()
		task.wait(7)
	end)
end
task.wait(7)
1 Like

Those waits are the problem. Since you are changing one tv at a time in your for loop, it needs to wait for a whole cycle before it can move to the next tv.

The solution is either to run each tv in its own function and use coroutines, or slightly change the design of the system to more strategically place the task.waits.

I tend to prefer the latter, so that’s what I’ll display here.

local SCW = game.Workspace.Map.CCI.SCW
local Folder = SCW.Script
local Sound = Folder.CheckUp.SoundBlock.Camp

local messages = {
    "RECOLHIMENTO",
    "TEMPO LIVRE",
    "REFEIÇÃO",
    "CHECKUP",
    "TEMPO LIVRE",
    "CHECKUP",
    "PRÉ-LOCKUP",
    "LOCKUP",
}

local TVs = {}
for i, v in pairs(Folder:GetDescendants()) do
	if v:IsA('TextLabel') and string.find(v.Name, "Time") then
		table.insert(TVs, v)
	end
end

while true do
    for _, message in ipairs(messages) do
	    for i, v in ipairs(TVs) do
		    v.Text = message
	    end
        sound:Play()
        wait(7)
    end
end
3 Likes

It works but it comes with an error as because of the waits - > 16:13:35.166 Script timeout: exhausted allowed execution time - Servidor - CheckUpTime:16

1 Like

Don’t forget the task.wait(7) at the end

1 Like

Yep, forgot about that one.
||||||||||||||||||||||||||||||||||\

1 Like

Actually, nobody of them now changes their text.

local SCW = game.Workspace.Map.CCI.SCW
local Folder = SCW.Script
local Sound = Folder.CheckUp.SoundBlock.Camp

local TVs = {}
for i, v in pairs(Folder:GetDescendants()) do
	if v:IsA('TextLabel') and string.find(v.Name, "Time") then
		table.insert(TVs, v)
	end
end

while true do
	for i, v in ipairs(TVs) do
		task.spawn(function()
			v.Text = "RECOLHIMENTO"
			Sound:Play()
			task.wait(7)
			v.Text = "TEMPO LIVRE"
			Sound:Play()
			task.wait(7)
			v.Text = "REFEIÇÃO"
			Sound:Play()
			task.wait(7)
			v.Text = "CHECKUP"
			Sound:Play()
			task.wait(7)
			v.Text = "TEMPO LIVRE"
			Sound:Play()
			task.wait(7)
			v.Text = "CHECKUP"
			Sound:Play()
			task.wait(7)
			v.Text = "PRÉ-LOCKUP"
			Sound:Play()
			task.wait(7)
			v.Text = "LOCKUP"
			Sound:Play()
			task.wait(7)
		end)
	end
	task.wait(7)
end

1 Like

Try this solution it utilizes tables and should be easier to read

2 Likes

Works.

image
image

2 Likes

Thanks, it worked 100%.
||||||||||||||||||||||||||||\\

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.