In both cases, you are looping through the children of “gunsFile”.
Say there are 5 children in gunsFile.
local totalCount = 0
for i, v in pairs(gunsFile:GetChildren()) do
totalCount = totalCount + 1
end
local collectedCount = 0
for i, v in pairs(gunsFile:GetChildren()) do
collectedCount = collectedCount + 1
end
The collected and total guns will be the same as you are looping through the children of the same folder. Maybe you wanted to loop through the gunscollected in the latter loop?
Try this:
local gunsCollected = player:WaitForChild(“gunsCollected”)
local totalCount = 0
for i, v in pairs(gunsFile:GetChildren()) do
totalCount = totalCount + 1
end
local collectedCount = 0
for i, v in pairs(gunsCollected:GetChildren()) do
collectedCount = collectedCount + 1
end
collected.Text = collectedCount…"/"…totalCount…" FOUND"
Also, I believe the code won’t update if a new gun is collected and it would still say the old amount. You can use .ChildAdded to update it as well.
local gunsCollected = player:WaitForChild(“gunsCollected”)
local collectedCount = 0
gunsCollected.ChildAdded:Connect(function(child) -- runs when something is added in the collected folder
collectedCount += 1
collected.Text = collectedCount…"/"…totalCount…" FOUND"
end)
Oh my lord, thank you so much. It does work. I tried this script it didn’t work, but all of a sudden it works now, I know I’m looping through the same folder. But I didn’t know what to do, because it didn’t work.
Yeah, so basically whenever I add a new gun for example: I have now 5 guns, I add one I have 6 guns. And I for example only collected 4. It would say 4/6 FOUND
local gunsCollected = player:WaitForChild(“gunsCollected”)
local totalCount = #gunsFile:GetChildren() -- number of children present in gunsFile
collectedCount = 0
for i, v in pairs(gunsCollected:GetChildren()) do
collectedCount += 1 --
end
collected.Text = collectedCount .. "/" .. totalCount .. " FOUND" -- sets the text once
gunsCollected.ChildAdded:Connect(function(child) -- runs when a child is added in gunsCollected
collectedCount += 1 -- increases collectedCount
collected.Text = collectedCount .. "/" .. totalCount .. " FOUND" -- updates the UI text
end)
I know it doesn’t apply in this scenario but if you’d want to subtract collectedcount and change the text whenever it is removed - copy and paste the whole “gunsCollected.ChildAdded” etc and replace Added with Removed and replace the plus with minus. I know it’s obvious but just in case someone stumbles across this and wants to subtract as well.