Adding values/numbers together based by what the Part's StringValue.Value is then displaying the total inside a Billboard GUI?

Alright, I am trying to make a part collecting billboard GUI pop up system that displays a number total based by color (part’s StringValue.Value)

Meaning, if there were 10 parts being collected at once, 5 were red and the other 5 were blue then it would add the 5 red parts together and displaying its total then adding the 5 blue parts together and also displaying its total separately. (like +5 red gui and +5 blue gui)

Anywho, the problem I am facing is that my code is not grouping these parts together by the color that my RNG script assigned them. Instead its displaying a Billboard Gui for EVERY part being collected, when Im trying to get them grouped, added together, then showing the combined total.

Example of whats wrong here.
RobloxScreenShot20200504_185112302

What I wanted is the orange to be +6 and the brown to be +4 instead of whats shown.

Right now I am putting the parts into a table, then using a for in pairs loop to loop through the table and using if/ elseif statements to see what StringValue(color name) the part’s decal has and trying to add those together to a variable. Then after the loop, if there was an added total more than 0, then it generate a Billboard gui and display the total. Then after that I set the totals back to 0 and make every key in the table nil with another in pairs loop.

here is the part of the script I am having trouble with. Not sure my logic here is correct. (Parts were detected with an invisible explosion)

--- code that I need help with here \/
elseif Kibble.Digs.Value <= 15 and Kibble.Digs.Value > 0 then
			
			--- the table for adding by colors
			local KibbleTable = {}
			--------------------------------
			
			table.insert(KibbleTable, Kibble)
			
			
			wait(KibbleWait.Value)
		
		
            --- blah irrelevant code blah
		   ---
		   ----------------------------------------
		
			
			local BrownTotal = 0
			local OrangeTotal = 0
			
			
			for _, KibbleT in pairs(KibbleTable) do
				
				print(KibbleT)
				
				local KibbleMultipliedT = KibbleT:FindFirstChildWhichIsA("Decal"):FindFirstChildOfClass("IntValue")
				local KibbleTextColorT = KibbleT:FindFirstChildWhichIsA("Decal").TextColor -- StringValue
				
				-- Browns
				if KibbleTextColorT.Value == "Brown" then 
					
					BrownTotal = BrownTotal + KibbleGive.Value * KibbleMultipliedT.Value -- KibbleGive is IntValue inside tool, multiplied is IntValue inside decal of Kibble part
			
				
				-- Oranges
				elseif KibbleTextColorT.Value == "Orange" then
					
					OrangeTotal = OrangeTotal + KibbleGive.Value * KibbleMultipliedT.Value
																					
			end
			end
				
				
				
				
				
				if BrownTotal > 0 then
					
					local HitmarkerClone = ServerStorage:WaitForChild("Hitmarker"):Clone() --- will be making this local later, testing on server for now
		
					HitmarkerClone.ExtentsOffsetWorldSpace = Vector3.new(0, (math.random(-70,170)/100), (math.random(-100,100)/100))
			
					HitmarkerClone.Frame.TextLabel.Text = "+"..BrownTotal
		
					HitmarkerClone.Frame.TextLabel.TextColor3 = Color3.fromRGB(147,102,39)	
					
					HitmarkerClone.Parent = player.Character
					
					wait(1)
					
					HitmarkerClone:Destroy()
					
				end
				
				
				
				
				if OrangeTotal > 0 then
					
					local HitmarkerClone = ServerStorage:WaitForChild("Hitmarker"):Clone()
		
					HitmarkerClone.ExtentsOffsetWorldSpace = Vector3.new(0, (math.random(-70,170)/100), (math.random(-100,100)/100))
			
					HitmarkerClone.Frame.TextLabel.Text = "+"..OrangeTotal
					
					HitmarkerClone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255,140,0)
					
					HitmarkerClone.Parent = player.Character
					
					wait(1)
			
					HitmarkerClone:Destroy()	
					
				end
				
				
				
				
				
	
			BrownTotal = 0
			OrangeTotal = 0
		
			
			for k in pairs(KibbleTable) do
				KibbleTable[k] = nil
			end
		
		
		
					

end
end
end)
end

I made a post for this last night, but I thought I didnt explain it well so I took it down and made a fresh post.

TY for reading o/

1 Like

Some suggestions / comments:

1.
Instead of doing this:

	local HitmarkerClone = ServerStorage:WaitForChild("Hitmarker"):Clone()
	-- blah irrelevant code blah ;-)
	wait(1)
	HitmarkerClone:Destroy()	

You should look into using the Debris service, for automatically “cleaning up / removing” parts after some time-interval.

So a better implementation of the above, could be (call to game:GetService("Debris") is not shown here):

	local HitmarkerClone = ServerStorage:WaitForChild("Hitmarker"):Clone()
	-- blah irrelevant code blah ;-)
	Debris:AddItem(HitmarkerClone, 1) -- Destroy the part after 1 second.

2.
Looking at the code you posted, there is no need to have this “resetting of local variables”:

	BrownTotal = 0
	OrangeTotal = 0

	for k in pairs(KibbleTable) do
		KibbleTable[k] = nil
	end

As you have BrownTotal, OrangeTotal and KibbleTable as local variables within the scope of that if-statement’s body, when leaving that scope, those local variables will automatically be disposed of.

So unless you have more “irrelevant code” not shown there, then I suggest you just remove those “resetting of local variables” statements.

3.
Now this part confuses me (probably because you did not include any of the “irrelevant code”?):

	local KibbleTable = {}
	table.insert(KibbleTable, Kibble)

	wait(KibbleWait.Value)

	--- (apparently not relevant code here?)

	for _, KibbleT in pairs(KibbleTable) do

What is the reason for that wait() statement there? Why do you need to “wait” after having added (just one) element to that local KibbleTable array, before iterating through it with the for loop?

1 Like
  1. Yes, I am aware of the debris server. Debris and what I did does the exact same thing. Not too worried about it.

  2. its a bad habit of mine to make double checks for things like that

  3. the wait is an IntValue inside of the tool being used. Its just an delay for the tool so it isnt collecting right away. Yes, putting it above the table helps but doesnt fix my whole problem.

Also the irrevelant code is about stats increasing, cframing, and changing intvalues. Didnt put in to confuse people.

Uhm, not really, as there is a slight difference between your implementation using a wait(1) versus using Debris(part, 1).

Your code, using the wait(1) at two places, is actually waiting one second before continuing to the next statement. So when both BrownTotal and OrangeTotal has a value greater than zero, then your code will do:

  • Clone a hitmarker for ‘Brown’ and show it
  • Wait one second…
  • Destroy that hitmarker
  • (after that waiting second) Clone a hitmarker for ‘Orange’ and show it
  • Wait one second…
  • Destroy that hitmarker

So you basically waits one whole second, before the OrangeTotal is being shown (maybe it is intended, I do not know. But it looks odd to me.)

By using the Debris service, and removing that wait(1) statement at both places, what would happen is:

  • Clone a hitmarker for ‘Brown’ and show it
  • Add it to the debris service, telling it to dispose/destroy it after one second
  • Clone a hitmarker for ‘Orange’ and show it
  • Add it to the debris service, telling it to dispose/destroy it after one second
  • (some time passes, i.e. 1 second, Debris service will automatically dispose/destroy those two objects)

So by using the Debris service, you have made it slightly optimized, by avoiding using explicit wait statements.

2 Likes

Ok, well thank you for these tips. I moved the wait with the intvalue up before the table as it wasnt intended to be there in the first place. I implemented the debris service too. More optimization is always good. Besides that, my main problem still persists…

Still trying to figure out why its cloning a Billboard for every part collected instead of making 2 billboard, one for orange total and one for brown total.

Ope, I managed to figure out what was wrong. After getting some caffeine in me, I looked through my code and realized I was overthinking it. I didn’t need any tables or loops since explosion gets all the parts that it hits. I also moved the 2 total variables and the two hitmarker if statements out of the function’s scope

Pretty silly of me tbh. With only close to 4 months of scripting practice, I’ll use this as a lesson to check where everything is at better lol.

@Decker004 after fixing the code, using

    wait(1)
	HitmarkerClone:Destroy()

did cause problems, so once again, thank you for mentioning that issue.

RobloxScreenShot20200505_185301399

tada

1 Like