Problems with all children being set to a specific text when they shouldn't

Basically got these 4 buttons


And when you press on one that says select, it should change the Selected button to Select

However it just sets both to say selected

PlayersHouse, PlayersHouses = CheckHouses:InvokeServer()
			
for i, v in pairs(Frame:GetChildren()) do
	if v:IsA('ImageButton') then
		if Price.Text == 'SELECT' then
			if v.Name == PlayersHouse then
			    v.Bottom.Price.Text = 'SELECTED'
				v.Bottom.Price.TextColor3 = Color3.fromRGB(255, 255, 255)
			else
				v.Bottom.Price.Text = 'SELECT'
				v.Bottom.Price.TextColor3 = Color3.fromRGB(255, 255, 255)
			end
		end
	end
end
2 Likes
if Price.Text == 'SELECT' then

This If statement says “only do the following stuff if Price.Text already says SELECT”.

So yes, the button that says SELECTED will fail that check and will not do any of things inside the scope of this If statement. The only place you change the value assigned to .Text is nested inside that parent If statement, so it will never change.

Not clear on the context or reason for the following check:

if v.Name == PlayersHouse then

as this doesn’t appear to add value at this point in the code. Consider removing this line and one corresponding “end”?

Your loops are nested wrong, so it only changes the text of anything named SELECT.

Here is how it should be written.

for i, v in pairs(Frame:GetChildren()) do
	if v:IsA('ImageButton') then
		if Price.Text == 'SELECT' then
			if v.Name == PlayersHouse then
			    v.Bottom.Price.Text = 'SELECTED'
				v.Bottom.Price.TextColor3 = Color3.fromRGB(255, 255, 255)
            end
		    else
				v.Bottom.Price.Text = 'SELECT'
				v.Bottom.Price.TextColor3 = Color3.fromRGB(255, 255, 255)
			end
		end
	end
end
1 Like

Basically, if v.Name == PlayersHouse checks if the button is the players current selected house, and thus changes the Price to be Selected (as that’s the players selected houe)

Just made it worse. Now the cost ones get changed to Select and it doesn’t change the Selected also

Then change else to elseif Price.Text = “SELECTED”.

If that does not work then there must be an error in your ClassName checks.

This should be:

for i, v in pairs(Frame:GetChildren()) do
	if v:IsA('ImageButton') then
		if Price.Text == 'SELECT' then
			if v.Name == PlayersHouse then
			    v.Bottom.Price.Text = 'SELECTED'
				v.Bottom.Price.TextColor3 = Color3.fromRGB(255, 255, 255)
                        end
	         else
		        v.Bottom.Price.Text = 'SELECT'
			v.Bottom.Price.TextColor3 = Color3.fromRGB(255, 255, 255)
		end
	end
end

Edit: Sorry about the formatting, can’t seem to tab on PC.

Didnt change anything

Is Price.Text not supposed to be v.Bottom.Price.Text?

Is there any reason why one part says Price.Text and the other says v.Bottom.Price.Text

Price is already mentioned higher up in the script

Oh, okay - so Price.Text would be a separate TextLabel?

for i, v in pairs(Frame:GetChildren()) do
				if v:IsA('ImageButton') then
					if Price.Text == 'SELECT' then
						if v.Name == PlayersHouse then
						    Price.Text = 'SELECTED'
							Price.TextColor3 = Color3.fromRGB(255, 255, 255)
			            end
					elseif Price.Text == 'SELECTED' then
						Price.Text = 'SELECT'
						Price.TextColor3 = Color3.fromRGB(255, 255, 255)
					end
				end
			end

This still doesnt work, now it just does nothing

Change Price.Text to v.Bottom.Price.Text

It is not working because you are using a different variable than the one attached to the object.

When using a for loop you must always reference the specific object, you cannot have one variable for the whole thing.

1 Like
for i, v in pairs(Frame:GetChildren()) do
    if v:IsA('ImageButton') then
        if v.Name == PlayersHouse then
            v.Bottom.Price.Text = 'SELECTED'
            v.Bottom.Price.TextColor3 = Color3.fromRGB(255, 255, 255)
        else
            v.Bottom.Price.Text = 'SELECT'
            v.Bottom.Price.TextColor3 = Color3.fromRGB(255, 255, 255)
        end
    end
end

You don’t need to check “if Price.Text == ‘SELECT’ then”.

That still changed nothing…

What is the name of the object that has the Text needs to be changed, because clearly there is an error while getting the Text.


That starting to get closer, but it also changes all of them

It changes all of them because none of them meet the parameters of that if statement. There is an error in another part of your system.

There’s no errors tho
30 characters