Hello all!
I have this little function within a module script that is intended to check if the folder is empty every time a child is removed from the folder. It works fine with the first folder, but as soon as I increase the counter variable by 1 and move on to the next folder, for some reason it doesn’t detect when a child is removed even though they are 100% being removed.
The module script within every tycoon:
local Buttons = {}
local Team = script.Parent.SpawnLocation.TeamColor
local player = nil
local totalButtons = 1
local BuyParts = script.Parent.CargoContainer:GetChildren()
local Folders = {script.Parent.CargoContainer1, script.Parent.CargoContrainer1Shops} --Change this when you add more button groups and stuff
local currentFolder = 1
local MPS = 0
game.Teams["Vorld Wision"].PlayerAdded:Connect(function(plr)
player = plr
end)
function Buttons.Appear(mod)
for _, v in pairs(mod:GetDescendants()) do
if v:IsA("Part") or v:IsA("MeshPart") then
if v.Name ~= "HumanoidRootPart" then
v.Transparency = 0
v.CanCollide = true
v.CanTouch = true
end
end
end
end
function Buttons.CheckIfCanBuy(Cost, plr)
if (game.Players:GetPlayerFromCharacter(plr.Parent).TeamColor == script.Parent.SpawnLocation.TeamColor) and (game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value >= Cost) then
game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value = game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value - Cost
return true
else --for some reason this block of an iff statement causes an error that doesnt acutally affect anything, but its really annoying to see in the output
return false
end
end
function Buttons.FindOwner()
return player
end
function Buttons.RevealButton(Folder) --the group of buttons you want to make appear
for _, v in pairs(Folder:getChildren()) do
for _, v in pairs(Folder:getDescendants()) do
if v:IsA("Part") then
v.Transparency = 0
v.CanCollide = true
v.CanTouch = true
else if v:IsA("BillboardGui") then
v.Enabled = true
end
end
end
end
end
function MPSS()
while true do
task.wait(5)
print("MPS Fired")
player:WaitForChild("leaderstats").Money.Value += MPS
print(currentFolder)
print(Folders[currentFolder]:getChildren())
end
end
Buttons.Appear(Folders[1].ABuyButton) -- for testing purposes, this is just to spawn in the first button for the chain
Folders[currentFolder].ChildRemoved:Connect(function()
print(#Folders[currentFolder]:GetChildren())
if #Folders[currentFolder]:GetChildren() == 0 then --Checks if the current folder is empty and if it is then move on to the next folder
currentFolder += 1
Buttons.RevealButton(Folders[currentFolder])
print(Folders[currentFolder])
else if Folders[currentFolder] == script.Parent.CargoContrainer1Shops then
MPS += 100
print("MPS INCREASED")
end
end
print("Child removed from")
print(Folders[currentFolder])
print(#Folders[currentFolder]:GetChildren())
end)
local never = true
local thread = coroutine.create(MPSS)
game.Teams["Vorld Wision"].PlayerAdded:Connect(function()
coroutine.resume(thread)
end)
return Buttons
The specific function within this module script:
Folders[currentFolder].ChildRemoved:Connect(function()
print(#Folders[currentFolder]:GetChildren())
if #Folders[currentFolder]:GetChildren() == 0 then --Checks if the current folder is empty and if it is then move on to the next folder
currentFolder += 1
Buttons.RevealButton(Folders[currentFolder])
print(Folders[currentFolder])
else if Folders[currentFolder] == script.Parent.CargoContrainer1Shops then
MPS += 100
print("MPS INCREASED")
end
end
print("Child removed from")
print(Folders[currentFolder])
print(#Folders[currentFolder]:GetChildren())
end)
The part that makes this so much more confusing for me is that I have conformation, from constant prints, that the children are being removed from the correct folder, AND that the counter variable isn’t broken. Literally any help would be much appreciated
I just replaced the whole Folder[currentFolder] with a variable to keep it a lil more clean but it didn’t change anything.
CF.ChildRemoved:Connect(function()
print(#CF:GetChildren())
if #CF:GetChildren() == 0 then --Checks if the current folder is empty and if it is then move on to the next folder
currentFolder += 1
CF = Folders[currentFolder]
Buttons.RevealButton(CF)
print(CF)
else if CF == script.Parent.CargoContrainer1Shops then
MPS += 100
print("MPS INCREASED")
end
end
print("Child removed from")
print(CF)
print(CF:GetChildren())
end)
I’m sorry, but where exactly in the code do you want me to put it? I tried putting it after the three prints and also after the entire CF.ChildRemoved and both times, the buttons never even showed.
CF.ChildRemoved:Once(function()
print(#CF:GetChildren())
if #CF:GetChildren() == 0 then --Checks if the current folder is empty and if it is then move on to the next folder
currentFolder += 1
CF = Folders[currentFolder]
Buttons.RevealButton(CF)
print(CF)
else if CF == script.Parent.CargoContrainer1Shops then
MPS += 100
print("MPS INCREASED")
end
end
print("Child removed from")
print(CF)
print(CF:GetChildren())
end)
If ‘Once’ doesn’t work then store the connection in a variable then disconnect it at the bottom of the function
Thanks for your code! unfortunately the CF.ChildRemoved:Connect(function()) is the main issue. What happens is that it registers when I remove a child from Folders[currentFolder] (otherwise known as CF) when currentFolder is at 1, but after the first if statement fires and it changes currentFolder to 2, sets CF to the updated version, and reveals the correct folder. It doesn’t recognize when I remove a child ever again. Here is the Folders list btw : local Folders = {script.Parent.CargoContainer1, script.Parent.CargoContrainer1Shops} - these are valid
Even stranger update: I decided to add this little silly piece of code to a function that constantly fires
wait(1)
local part = Instance.new("Part")
print("Part Created")
part.Parent = Folders[1]
wait(2)
part:Destroy()
print("part destroyed")
It turns out that the CF.ChildRemoved line still recognizes CF as CargoContainer1. I am literally speechless, this makes my brain hurt. I assume that the solution would be to update CF in a way that ChildRemoved is ok with but Im not sure how.
You could perhaps refresh the connection? Like this
function ChildRemoved(CF)
CF.ChildRemoved:Connect(function()
print(#CF:GetChildren())
if #CF:GetChildren() == 0 then --Checks if the current folder is empty and if it is then move on to the next folder
currentFolder += 1
Cf = Folders[currentFolder]
ChildRemoved(Cf) — Register the new connection
Buttons.RevealButton(CF)
print(CF)
else if CF == script.Parent.CargoContrainer1Shops then
MPS += 100
print("MPS INCREASED")
end
end
print("Child removed from")
print(CF)
print(CF:GetChildren())
end)
end
ChildRemoved() — Call it one to register the connection
I thought of this before this but I couldn’t type out my response since I was in school
Ok here’s my guess, since I don’t face these problems much…
I think that the event will only detect for the instance that it was first assigned to and even if you change it, that event sort of has…an attribute…to detect the child removal for that instance only. So you need to refresh the connection to tell that event to start detecting for a new instance instead