Tracking the number of specific objects

robloxapp-20200801-0141035.wmv (286.6 KB)

  1. What do you want to achieve? I want to keep track of number of frames with green background just like in the video above. All square frames are stored inside one frame called StorageHolderFrame. Shield gui dragging works by using UserInputService.InputChanged event and holding left mouse button. When a shield collides with square frames, their color changes to green, when not, it’s white.

  2. What is the issue? I just don’t know how to solve it.

  3. What solutions have you tried so far? I tried using table/loop to store green frames just like in script below but the result was always wrong.
    The script below is located inside UserInputService.InputChanged event. StorageHolderFrame is a frame where all square frame slots are.

for i,v in pairs(script.Parent.StorageHolderFrame:GetChildren()) do
	if v.BackgroundColor3 == Color3.fromRGB(0,255,0) then
		count = count + 1
	end
end
print(count)

But the problem is that everytime a Shield gui position changes by even 1 pixel, the count variable increases by 1. So after like 1 second of dragging, count value is over 100. I want to track exact amount of green slots.
I’m not giving code of how it all works so I’m not expecting code with complete solution. I just want a tips how to achieve it.

I am in a hurry typing this, but maybe detect when you enter a frame. When you do, add it to a table and each time you detect a frame you must check if it already exists in the table.
Remove the frame when you are no longer hovering on it.

On a second thought, just try resetting the count variable at the start of the input event.

local Storage = {}

for i,v in pairs(script.Parent.StorageHolderFrame:GetChildren()) do
	if v.BackgroundColor3 == Color3.fromRGB(0,255,0) then
		table.insert(Storage, v)
	end
end

print(table.getn(Storage))

You can access the items in the table by:

print(Storage[1]) -- Will return the first item in the table unless the first item is nill

or

for Index, Frame in pairs(Storage) do
	print(Index, Frame) -- Prints the Index for each item in the table along with the Item itself
end

Wow, resetting count actually works perfectly. Thats pretty clever. Thanks very much

No problem, this is my first question solved actually…