Checking if one elements text is something else than others

I’m trying to create some sort of system where I check if one element with the same name of some frame’s text is “Select” if it isn’t I return false to let them now the player has already selected something.

Current script: (Local)

local function check()
	for i,v in pairs(ClassFrames:GetChildren()) do
		if v.Frame.statusText.Text ~= "Select" then
			return true
		else
			return false
		end
	end
end

I’m not sure if this is going to return false for just one… since it runs through every element with the text. Something will return false and some true, how do I activate if just one of all the elements returns true.

Please write suggestions or code in the replies.

It’s because when you run the return inside a for i v in pairs loop, you are cancelling the loop and continuing the script which will end the function. So maybe add a value inside the function to see if it’s true, and at the end it can see “if value == true then blah blah”

local function check()
local checker = false
	for i,v in pairs(ClassFrames:GetChildren()) do
		if v.Frame.statusText.Text ~= "Select" then
            checker = true
		end
	end
if checker == true then 
return true 
else 
return false
end
end

How would I then check if it’s false or true in another function?

script.Parent.SelectBtn.MouseButton1Click:Connect(function()
	
	if check() == false then
		print("No classes selected")
	else
		print("fortnite")
	end
	
end)

Would this work? Since Im not getting a result

You would have to do

script.Parent.SelectBtn.MouseButton1Click:Connect(function()
	local check = check()
	if check == false then
		print("No classes selected")
	else
		print("fortnite")
	end
	
end)

As @SOA_hydra said, if you want to check 2 (or more) conditions, you’d prob need a return or something that can result in several ends or something that can actually have different conditions. In this case, you’d check the text , and see if it fits to your requirement. If you only have 2 conditions, you can work with if and else, if you have more options you want to take consideration in, you’d use elseif too,

To conclude, I didn’t see your entire code, but if you’re trying to invoke an event from client to server, use the remote function and make sure to put the invoke as a variable [so that, you could use it more than once, by returning it from the server].
and you can continue from there.

[I hope i understood you right]

Still not getting any prints. Please note this is a surface gui I located in PlayerGui with the adornee set to the screens part. With a local script guiiding it’s buttons.

So you want to invoke the server everytime a seat has been taken by a player?

No i’m checking this on the client. It might not be the safest but it’s the path i chose. My script right now.

local function check()
	local checker = false
	for i,v in pairs(ClassFrames:GetChildren()) do
		if checker.Value == false then
			if v.Frame.statusText.Text ~= "Select" then
				checker = true
			end
		end
	end
	if checker == true then 
		return true 
	else 
		return false
	end
end


script.Parent.SelectBtn.MouseButton1Click:Connect(function()
	local check = check()
	if check == false then
		print("No classes selected")
	else
		print("fortnite")
	end

end)

(All the variables are mentioned earlier in the script)

I know that other buttons work as I have functions with buttons aswell. Just this one doesnt seem to work.

I think you should do that from the server[ by using an event/invoke], and it could help you.

Because, if I understood you right,
You need to update the server when the client chooses a seat or something [ otherwise, for others, they would see your seat as ‘free to take’, while for you it’s taken.]

Try using a remote function to communicate with the server & client, it should help that

Hold on, give me a few minutes.

local function check()
	for i,v in pairs(ClassFrames:GetChildren()) do
		if v.Frame.statusText.Text ~= "Select" then
			return true
		end
	end
	return false
end

If the text of any status is “Select” the function will return true, if no status has the text “Select” then the function will return false.

You can update the amount of seats remaining with this remote function aswell.

How:
You can make a global value for how many seats are available from the start.
Everytime a player takes a seat, you should invoke this to the server, and reduce 1 from amount of available chairs to be now.[ You can of course use this to limit the players from selecting/getting a seat - choose for them a seat that hasnt been taken yet].

You can update that text by checking when the global value of ‘available chairs’ has chaged , or more ways you would like to use.

As @Forummer mentioned here, if you’re trying to check when your text is different from ‘Select’, you can return true to the server/client, and do stuff on that condition.

If there hasnt been found any status with the text ‘Select’, you should / want to return false and do stuff on that condition, for e.g - dont let the player choose a seat or something alike.

Not sure what i’m doing wrong here.

Server Script:

script.Parent.Invoke.OnServerInvoke = function(plr)

	local checker = false
	for i,v in pairs(plr.PlayerGui.SCI.MainFrame.CheckInFrame.AttributeFrame.ClassFrames:GetChildren()) do
		if checker.Value == false then
			if v.Frame.statusText.Text ~= "Select" then
				checker = true
			end
		end
	end
	if checker == true then 
		return true 
	else 
		return false
	end
	
end

Local script:

script.Parent.SelectBtn.MouseButton1Click:Connect(function()
	local result = script.Parent.Invoke:InvokeServer()
	if result == false then
		print("No classes selected")
	else
		print("fortnite")
	end

end)

Still not getting any prints.

Check what @Forummer said above

So how would I stop when it finds a label that has a text different than all the others.

local function stopAtDifferentValue(f) --"f" is a folder of value instances.
	local constantValue = nil
	for _, valueInstance in ipairs(f:GetChildren()) do
		if valueInstance:IsA("ValueBase") then
			if not constantValue then
				constantValue = valueInstance.Value
			else
				if constantValue ~= valueInstance.Value then
					return valueInstance
				end
			end
		end
	end
end

Here’s a function which I believe demonstrates what you’re trying to achieve.

It returns the first value instance in a folder of value instances which has a value different from the others.

1 Like

What does this return exactly?

local function stopAtDifferentValue(f) --"f" is a folder of value instances.
	local constantValue = nil
	for _, valueInstance in ipairs(f:GetChildren()) do
		if valueInstance:IsA("ValueBase") then
			if not constantValue then
				constantValue = valueInstance.Value
			else
				if constantValue ~= valueInstance.Value then
					return valueInstance
				end
			end
		end
	end
end

script.Parent.SelectBtn.MouseButton1Click:Connect(function()
	local result = stopAtDifferentValue(ClassFrames)
	print(result)
end)

What does this do? Like what does it return? Since it returns valueInstance