How to remove an item for a dictionary?

I’m trying to make a debounce whenever an explosion of a fireball hits a player and makes a debounce for each player that was hit.

local explosion_debounces = {}

local function Enter_Player_Debounce(debounce_table, player_Name)
	table.insert(debounce_table, player_Name)
	task.wait(1)
	debounce_table[player_Name] = nil
end

Here I passed in one of the debounce tables and the player’s name as the parameters then after one second it should remove the player’s name from the dictionary but it didn’t work even though it should’ve. I don’t know how to solve this?

1 Like

table.insert inserts only in integer, value pairs. This means every time you add something to the table using table.insert, the index is the size of the dictionary + 1. You would instead want to use:

debounce_table[player_Name] = true

Oh uh… about that… I didn’t create a table whenever a player joined (I’m new to this “advanced” debounce thing) I thought I could just insert a player’s name and then remove it

What ede means is table.insert(debounce_table, player_Name) inserts a number rather than the player’s name. Basically what the code above is doing is:

debounce_table = {
	[1] = "the player's name",
}

So you’re not actually creating a way to reference the player. As stated before by ede, you should just do debounce_table[player_Name] = true as this creates a reference to the player in the table.

debounce_table = {
	["the player's name"] = true,
}

Thus your new code would be:

local function Enter_Player_Debounce(debounce_table, player_Name)
	debounce_table[player_Name] = true
	task.wait(1)
	debounce_table[player_Name] = nil
end

If this works, and it should, give the check to ede2355, since all I did was just elaborate more on what he said

1 Like

Wait does debouce[player_Name] = true function like table.insert because it doesn’t exist in the table and creates it?

You can try to use table.find() to get the position of the debounce located in the table

local explosion_debounces = {}

local function Enter_Player_Debounce(debounce_table, player_Name)
	table.insert(debounce_table, player_Name)
	task.wait(1)
    local index = table.find(explosion_debounces,player_name)
    if index ~= nil then
        table.remove(explosion_debounces, index)
    end
end

Though I don’t recommend this method.

That looks complicated ngl lol

I’m not quite sure what you you might want explain a bit further. But debouce[player_Name] = true sets whatever index, in this case that theplayer_name, to true. So if the index already exists in the table, itll just replace the index value to true

It starts out as an empty table though

@dzhann757

debounce_table = {
	["name"] = true,
}
debounce_table["this_name"] = false

print(debounce_table.name) --> true
print(debounce_table.other_name) --> nil, because other_name does not exist in the table
print(debounce_table.this_name) --> false

Oh wait, I think I understand it now kind of

local explosion_debounces = {}

local function Enter_Player_Debounce(debounce_table, player_Name: string)
	debounce_table[player_Name] = true
	task.wait(1)
	debounce_table[player_Name] = nil -- This will remove the player's debounce from the (debounce_table)
end

Refined code If you want to use it.

If you’re only using explosion_debounces for the argument then you should do this instead:

local explosion_debounces = {}

local function Enter_Player_Debounce(player_Name: string)
	explosion_debounces[player_Name] = true
	task.wait(1)
	explosion_debounces[player_Name] = nil
end

Okay, I will use it! Wait, who do I give the check mark to?

Depends on who solve your problem.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.