Checking 2 seperate frames causing problems

I have a frame called ‘Colors’ and under it two more, 1 called ‘Free’ the other called ‘Premium’

Basically, these both serve the same purpose, only different being the premium colors can be selected if you own a gamepass. Problem I am having is well 1. The code is basically done twice, with only the gamepass being the big difference, so I feel there has to be a way to tidy this up, but also 2. the main problem is I have an icon called ‘Selected’ that is inside one of the colors (the selected color) to kinda show that you have the color selected. However, if the selected icon is a child of one of the ‘Free’ frames children, and you click on a ‘Premium’ color then it errors, as it can’t find the Selected icon under the Premium icons (as it’s in one of the free ones)

Paint:GetPropertyChangedSignal('Visible'):Connect(function()
	for _, v in pairs(Colors.Free:GetDescendants()) do
		if v.Name == 'Selected' then
			v.Parent = Colors.Free:FindFirstChild(SelectedColor.Name)
		end
		if v:IsA('ImageButton') then
			v.Activated:Connect(function()	
				local SelectedIcon = Colors.Free:FindFirstChild(SelectedColor.Name):FindFirstChild('Selected')
				SelectedIcon.Parent = v
				
				UI:FindFirstChild(SelectedSection).Texture.ImageColor3 = v.ImageColor3
				SelectedColor = BrickColor.new(v.ImageColor3)

				PaintPlot:FireServer(SelectedSection, SelectedColor)
			end)
		end
	end
	
	for _, v in pairs(Colors.Free:GetDescendants()) do
		if v.Name == 'Selected' then
			v.Parent = Colors.Premium:FindFirstChild(SelectedColor.Name)
		end
		if v:IsA('ImageButton') then
			v.Activated:Connect(function()
				if v.Parent == 'Premium' then 
					if not MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 5054762) then return end
				end
				
				local SelectedIcon = Colors.Premium:FindFirstChild(SelectedColor.Name):FindFirstChild('Selected')
				SelectedIcon.Parent = v
				
				UI:FindFirstChild(SelectedSection).Texture.ImageColor3 = v.ImageColor3
				SelectedColor = BrickColor.new(v.ImageColor3)

				PaintPlot:FireServer(SelectedSection, SelectedColor)
			end)
		end
	end
end)


Free colors being the ones on top, premium being the single greenish color. The selected icon is currently under the white circle (blue outline)

From my understanding you should probably just get rid of the top for loop all together; because the free options should still execute in the second loop, because their parent is not “Premium”

As this line can easily be edited:

v.Parent = Colors.Premium:FindFirstChild(SelectedColor.Name) or Colors.Free:FindFirstChild(SelectedColor.Name)
Paint:GetPropertyChangedSignal('Visible'):Connect(function()	
	for _, v in pairs(Colors.Free:GetDescendants()) do
		if v.Name == 'Selected' then
			v.Parent = Colors.Premium:FindFirstChild(SelectedColor.Name)
		end
		if v:IsA('ImageButton') then
			v.Activated:Connect(function()
				if v.Parent == 'Premium' then 
					if not MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 5054762) then return end
				end
				
				local SelectedIcon = Colors.Premium:FindFirstChild(SelectedColor.Name):FindFirstChild('Selected')
				SelectedIcon.Parent = v
				
				UI:FindFirstChild(SelectedSection).Texture.ImageColor3 = v.ImageColor3
				SelectedColor = BrickColor.new(v.ImageColor3)

				PaintPlot:FireServer(SelectedSection, SelectedColor)
			end)
		end
	end
end)

That still wouldn’t solve the issue of locating the ‘Selected’ icon between free and premium frames tho

Sorry, just edited my response because I saw that.

Paint:GetPropertyChangedSignal('Visible'):Connect(function()	
	for _, v in pairs(Colors.Free:GetDescendants()) do
		if v.Name == 'Selected' then
			v.Parent = Colors.Premium:FindFirstChild(SelectedColor.Name) or Colors.Free:FindFirstChild(SelectedColor.Name)
		end
		if v:IsA('ImageButton') then
			v.Activated:Connect(function()
				if v.Parent == 'Premium' then 
					if not MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 5054762) then return end
				end
				
				local SelectedIcon = Colors.Premium:FindFirstChild(SelectedColor.Name):FindFirstChild('Selected') or Colors.Free:FindFirstChild(SelectedColor.Name):FindFirstChild('Selected') -- ERROR HERE
				SelectedIcon.Parent = v
				
				UI:FindFirstChild(SelectedSection).Texture.ImageColor3 = v.ImageColor3
				SelectedColor = BrickColor.new(v.ImageColor3)

				PaintPlot:FireServer(SelectedSection, SelectedColor)
			end)
		end
	end
end)

(have to scroll to error line)

attempt to index a nil value
but if I do

print(SelectedColor.Name)
print(Colors.Free:FindFirstChild(SelectedColor.Name))

Bright orange

Bright orange

Could you please tell me a line number, that the error is on?

local SelectedIcon = Colors.Premium:FindFirstChild(SelectedColor.Name):FindFirstChild('Selected') or Colors.Free:FindFirstChild(SelectedColor.Name):FindFirstChild('Selected') -- ERROR HERE

Try changing that to

local SelectedIcon = Colors:FindFirstChild("Selected", true) -- As long as it's the only thing with that name it should be found.

I also noticed at the top of the for loop

it was like this in both for loops you had in the original post, I don’t know if this was intended, as you have Colors.Premium; if this was indeed not intended I’d try changing it to Colors:GetDescendants()

2 Likes