5/5 found problem (Solved)

So basically I have 5 guns in my game, but I only have collected 4, but it keeps saying 5/5, idk why?
image

image

Copy and Paste Script:

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(gunsFile:GetChildren()) do
collectedCount = collectedCount + 1
end

collected.Text = collectedCount…"/"…totalCount…" FOUND"

maybe a debounce will help your case.
or printing how many you have each time you collect one (to see when it jumps to 5)

I tried, none of those worked, unless I did those wrong

You could have some checks in those loops, also is that the file has 5 children?

Right now you are setting the collectedcount to the same count as the totalcount

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)
1 Like

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.

1 Like

It was working but now doesn’t sadly

Okay so,
How does this code work? Is it supposed to change the collected text every time a new gun is added into the gunscollected folder?

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)

The first try works, now let’s see if the second try works.

It does work, bro thank you so much, I don’t now how to repay you man…

Don’t worry about it.
Also, don’t forget to mark the topic as solved if you aren’t having the problem anymore.

yeah Ik I will do that sir.
(30 cars)

1 Like

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.

1 Like