One value from table

Hello,
I want to take a value from table and if it is the same as in handler then cancel the event but im getting trouble because it is comparing that value from handler to every ID in the table.


local Checked = {}

RegisterServerEvent('test123:checkValue')
AddEventHandler('test123:checkValue', function(id)
local _id = id
local src = source

		for i = 1, #Checked, 1 do
			print(Checked[i].id)
			print("our id ".._id)
			
			if _id == Checked[i].id then
			print("already open")
			else
			table.insert(Checked, {id = _id})
			print("you found something")
			end
		end

end)

My idea is to check if _id is already in that table (other player triggered that event and it was added to table so it could not be triggered again) I know im doing something wrong but im not sure what it is

I suggest you do it this way instead:

local Checked = {}

RegisterServerEvent('test123:checkValue')
AddEventHandler('test123:checkValue', function(id)
	if Checked[id] then
		print("Already checked")
	else
		Checked[id] = true
		print("You found a thing")
	end
end)

Typed on my phone apologies for any Syntax errors

1 Like

Thank you very much :slight_smile: worked just like i wanted

If you want some examples of how to manipulate arrays, here is a Server file from one of my resources, it does some pretty cool stuff:

1 Like

Nice this will definitely help in a future, also im suprised about table.remove becuase I heard that this would cause some problems and it is better to remove everything (like YourTable = {} )and add again everything without that one thing you want to remove. Anyway thanks again :slight_smile:

Interesting, I’ve never heard of this being an issue, nor encountered any issues with it myself. The reason I used table.remove() there specifically, is because that array contains string entries in a specific order, and all that event does is remove the last entry in the array, and it seemed easier to drop the last entry off of the array with table.remove(), then to loop through the entire array, only to remove the last entry anyway.