local minNum = 3
local maxNum = 5
local i = minNum
workspace.Box1.ChildAdded:Connect(function()
i += 1
print(i)
end)
for _ = minNum, maxNum do
print(i)
i += 1
end
local minNum = 3
local maxNum = 5
for i=minNum, maxNum do
print(i)
end
workspace.Box1.ChildAdded:Connect(function()
maxNum += 1
print(maxNum)
end)
If you just want to print the added number why go through the print loop again after the maxNum is increased? Just print it inside the ChildAdded function.
To achieve the desired behavior, you need to modify your loop structure. Currently, the loop is executed only once, and it uses the initial values of minNum and maxNum. However, you want the loop to dynamically update each time a child is added to Box1 in the workspace. Here’s an updated version of your code that should work as intended:
local minNum = 3
local maxNum = 5
local function printNumbers()
for i = minNum, maxNum do
print(i)
end
end
workspace.Box1.ChildAdded:Connect(function()
maxNum = maxNum + 1
printNumbers()
end)
printNumbers() -- Initial execution of the loop
In this updated code, I’ve extracted the loop into a separate function called printNumbers(). This function will be called both initially and each time a child is added to Box1. After updating maxNum, the printNumbers() function is called to print the updated sequence of numbers.
Note that the initial execution of the loop is done outside of the event connection to ensure the initial values are printed as well.