Table Debounce Issue

Alright so basically I have a table debounce where its like this, but problem is that the objects are the same name and its only working for one of them sadly

local Debounces = {}

if not Debounces[something.Name] then
      Debounce[something.Name] = true
       wait(5)
      Debounce[something.Name] = false
end

Just store the object itself in the table, and do this to remove the reference:

local Debounces = {}

if not Debounces[something] then
      Debounce[something] = true
       wait(5)
      Debounce[something] = nil
end

Instead of using their name use the reference itself. No two objects can be the same.

local Debounces = {}

if not Debounces[something] then
      Debounce[something] = true
      wait(5)
      Debounce[something] = nil
end

Notice that I set it to nil afterwards instead of to false, so that you don’t leak memory when the instance is destroyed

Wont that cause a memory leak over time because it wont get garbage collected

That is why you set the reference to nil, thus it can get garbage collected since it will be removed from the table, setting a index to nil removes it.

1 Like

Alright sounds good thanks for the insight let me try it really quickly if you dont mind

1 Like

Ooof I tried it and its still not working

Also, what you should really do is use a weak table to remove the reference when the object gets destroyed:

local Debounces = {}
setmetatable(Debounces, {__mode = "v"})

if not Debounces[something] then
      Debounce[something] = true
      wait(5)
      Debounce[something] = nil
end

Wait but im never destroying the object though

Ok then you don’t need it, but the solution should work.

Alright I just tried you 3nd solution and it didnt work also I dont know whats up with this, when I change the objects im referring to, to different names it works

Try the original post I updated it.

Yeah I tried that solution and it didnt work oof, @cjjdawg

This is the code

elseif not hit and not Stopped[car.Name] then
					for index,sign in pairs(stopSigns:GetChildren()) do
						if (car.Position - sign.PrimaryPart.Position).Magnitude < 11 then
							Stopped[car.Name] = true
							BodyVelocity.Velocity = Vector3.new(0,0,0) - car.CFrame.lookVector - Vector3.new(0,0,.005)
							wait(.2)
							BodyVelocity.Velocity = Vector3.new(0,0,0)
							print(car.Name.." has been stopped!")
							wait(1.2)
							BodyVelocity.Velocity = car.CFrame.LookVector * 11.1
							print(car.Name.." can now move!")
							wait(1.5)
							Stopped[car.Name] = nil
						else
							print(car.Name.." is accelerating . . !")
							BodyVelocity.Velocity = car.CFrame.LookVector * 11.1
						end
					end

Ensure the rest of your code is correct because two objects will all the same properties will be different because they point to different locations in memory.

@cjjdawg Any ideas whats up, because it only works with one car

sorry for the reply but your still doing car.Name as the index just do car

Clever, as accessing objects is faster than accessing (ex): its name.