Checking if any player has a value that matches the one being looked for by the Server..?

I have made a Mobile Data Terminal for my game for Law Enforcement Officers to use,
but I have ran into a problem when it came to checking if any of the players have a matching value.


Here’s how it works.
Once a player joins, the server generates a plate and stores it inside the player.
The officer inputs a Plate inside a Gui, and clicks a button, the Gui invokes the server and
it looks if any of the players has that same exact plate that the Officer input.


What’s the problem?
I’ve got everything working, thing is…
I have it set so if there is no plate detected, it shows an Error message,
well It does it for EVERY player that does not have a matching value, and that’s a problem
I want it to show the error once, if none of the players have a matching value.


I’ve tried debounce, and that didn’t work


Problematic Code

game.ReplicatedStorage.MDTEvents.LookupPlate.OnServerInvoke = function(Officer,Plate)
	local Players = game.Players:GetChildren()
	  for i = 1, #Players do
		if Players[i].Information.Plate.Value == Plate then
				print('Found')
			else
				print('No players matching')
			--  (This loops each time that it's not matching - I'd like it to do it ONCE if none are matching)
		end
	end
end
2 Likes

Have a Boolean flag for the for loop - set this to false initially, but then go through every player with the same check. If you find one where the player has the plate, set the value to true and do everything else with that. At the end of the loop, check the value - if it’s false, you had no matching plates.

Something like this:

local v = false
for every plate:
  if plate matches
    v = true

if not v:
  -- no match
4 Likes

You’ll want to set a value to true when a player with the same Plate has been identified.
You can then use break to exit out of the loop so you don’t loop through anymore.

After the loop, if the value is false then show the error message.

E.g.

value = false

loop here
    does the player have the plate?
    if yes, set value to true
        USER IS FOUND!
        Break
    if not, do nothing and keep looping
    end
end

if not value then (if value is false)
    NO USER FOUND!

Hope this helped! :smiley:

2 Likes

Thank you! this solution worked for me.