Accurate Workspace Coin Counter Problems, anyone can help?
I have coins that spawn randomly into the map. The coins have a script inside them that automatically despawns them after a random amount of time.
I have a separate script that is attempting to accurately keep track of the amount of coins in workspace through all the spawning and despawning and I'm at a loss at how to do this. The script is supposed to take this amount and put it into a number value.
I know what I have doesn't work but I'm not experienced in this kind of thing so any help would be appreciated. It does count coins but the numbers are way off.
03 | function AddCoinAmt() wait() |
04 | for _,v in pairs (workspace:GetDescendants()) do |
05 | if v:IsA( "Part" ) and v.Name = = "CoinPart" then |
06 | coinsadded = coinsadded + 1 |
09 | local coinvalue = script:FindFirstChild( 'CoinVal' ) |
10 | if coinvalue then coinvalue.Value = coinsadded - coinsremoved |
14 | function SubtractCoinAmt() wait() |
15 | for _,v in pairs (workspace:GetDescendants()) do |
16 | if v:IsA( "Part" ) and v.Name = = "CoinPart" then |
17 | coinsremoved = coinsremoved + 1 |
20 | local coinvalue = script:FindFirstChild( 'CoinVal' ) |
21 | if coinvalue then coinvalue.Value = coinsadded - coinsremoved |
25 | workspace.ChildAdded:connect(AddCoinAmt) |
26 | workspace.ChildRemoved:Connect(SubtractCoinAmt) |