So i have this script below, theres a color changing loop and i want it to apply on every part inside of the folder but when i run the script it works only on 1 part. Is there any ways to change the loop to something else or make the loop work on all parts inside? This is a script inside ServerScriptService
local folder = workspace.Servers
local Ecolor = BrickColor.new(“Lime green”)
local Dcolor = BrickColor.new(“Really red”)
local defaultColor = BrickColor.new(“Dark stone grey”)
local function loop(part, broken)
while true do
part.BrickColor = defaultColor
wait(0.35)
if broken.Value == false then
part.BrickColor = Ecolor
elseif broken.Value == true then
part.BrickColor = Dcolor
end
wait(0.35)
end
end
local function handleP(part)
local broken = part:FindFirstChild(“DisablS”)
loop(part, broken)
end
for i,v in pairs(folder:GetChildren()) do
if v:IsA(“Part”) then
handleP(v)
end
end
You need to store all of the parts in a table and use the table to execute your function on that part
local yourtable = {}
for i,v in pairs(folder:GetChildren()) do
if v:IsA("Part") then
table.insert(yourtable,v)
end
end
for i , v in ipairs(yourtable) do
handleP(v)
end
Also just like @stupidestaIive said below, remove the while true loop from the function
Tip: use ```lua to format code in the forum
The function is yielding, so the while loop runs before the for loop continues
You can do one of two things (not sure if they’ll work though)
remove the while loop and put the remainder of the function in a RenderStepped event (if it doesn’t work use Heartbeat)
--Example:
--insert variables
local function loop(part, broken)
part.BrickColor = defaultColor
task.wait(0.35)
if broken.Value == false then
part.BrickColor = Ecolor
elseif broken.Value == true then
part.BrickColor = Dcolor
end
task.wait(0.35)
end
--insert handleP function
for i, v in ipairs(folder:GetChildren()) do
if v:IsA("Part") then
game:GetService("RunService").RenderStepped:Connect(function()
handleP(v)
end)
end
end
Do not do this, you’re starting a heartbeat loop for every single part, if you’re going to do it this way, just do it like this.
local Folder = workspace.Servers
local Colors = {
Default = BrickColor.new("Dark stone grey"),
Enabled = BrickColor.new("Lime green"),
Disabled = BrickColor.new("Really red"),
}
for _, part in Folder:GetChildren() do
if not part:IsA("BasePart") then
continue
end
local isBroken = part:FindFirstChild("DisablS")
if isBroken == nil then
continue
end
local function checkState()
part.BrickColor = Colors.Default
task.wait(0.35)
if isBroken.Value then
part.BrickColor = Colors.Disabled
else
part.BrickColor = Colors.Enabled
end
end
task.spawn(checkState)
isBroken:GetPropertyChangedSignal("Value"):Connect(function()
checkState()
end)
end
i added a print(v) to check what parts are being added to the table and it just adds 1 part again, seems like im just dumb
local folder = workspace.Servers
local Ecolor = BrickColor.new("Lime green")
local Dcolor = BrickColor.new("Really red")
local defaultColor = BrickColor.new('Dark stone grey')
local yourtable = {}
local function loop(part, broken)
while true do
part.BrickColor = defaultColor
wait(0.35)
if broken.Value == false then
part.BrickColor = Ecolor
elseif broken.Value == true then
part.BrickColor = Dcolor
end
wait(0.35)
end
end
local function handleP(part)
local broken = part:FindFirstChild("DisablS")
loop(part, broken)
end
for i,v in pairs(folder:GetChildren()) do
if v:IsA("Part") then
table.insert(yourtable,v)
for i , v in ipairs(yourtable) do
print(v.Name)
handleP(v)
end
end
end
for i,v in pairs(folder:GetChildren()) do
if v:IsA("Part") then
table.insert(yourtable,v)
end
end
for i , v in ipairs(yourtable) do
print(v.Name)
handleP(v)
end
local folder = workspace.Servers
local Ecolor = BrickColor.new(“Lime green”)
local Dcolor = BrickColor.new(“Really red”)
local defaultColor = BrickColor.new(“Dark stone grey”)
local function loop(part, broken)
while true do
part.BrickColor = defaultColor
task.wait(0.35)
if broken.Value == false then
part.BrickColor = Ecolor
elseif broken.Value == true then
part.BrickColor = Dcolor
end
wait(0.35)
end
end
local function handleP(part)
local broken = part:FindFirstChild(“DisablS”)
spawn(function()
loop(part, broken)
end)
end
for i,v in pairs(folder:GetChildren()) do
if v:IsA(“Part”) then
handleP(v)
end
end