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.
- Location
- 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
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)
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
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
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