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.
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.