How to check if value is already in array?

so, here’s my code. when you touch a part, your name is added to a table. but it is supposed to only add your name once.

local win = game.ReplicatedStorage.Win
local module = require(game.ServerScriptService.Module)

winners = module.winners
local debounce = false
script.Parent.Touched:Connect(function(hit)
    if debounce == false then
	    debounce = true
	    if hit.Parent:FindFirstChild("Humanoid") then
		    if not winners[hit.Parent.Name] == true then 
		    	table.insert(module.winners,hit.Parent.Name)
		    	win:FireAllClients(hit.Parent.Name)
		      	print(winners)
		    	debounce = false
		    end
	    end
    end
end)

and as you can see, it’s adding my name every time.
image
how can I make it only add my name to the table once?

Dictionaries have a key-value pair, while Arrays have an index-value pair.

When you’re checking for if a value is stored for winners[hit.Parent.Name], this will be checking if there’s a given Key with that name in a Dictionary. However, on the next line you utilize table.insert to add a new value to the table, which is used for Arrays.

In this case, you can utilize table.find(tableName, valueToLookFor) to look through an Array for a given value, or you can use an ipairs loop and check if the value is already there.

5 Likes

Table have an array part and a dictionary part.
you can use the name of the player as the key, and use true as the value,

This is a simple way to do that, by using table.find which returns the index

if table.find(module.winners, hit.Parent.Name) then
    -- It's already in the table
end
1 Like