The cleanest way would be to use the functions meant to handle tables, such as table.insert, table.remove and table.find, in my opinion.
Here’s an example:
local Debounces = {}
if (table.find(Debounces, Player.UserId)) -- Check if player is already in the table.
then return end -- Return end if they already exist.
table.insert(Debounces, Player.UserId) -- Add player.
table.remove(Debounces, table.find(Debounces, Player.UserId)) -- Remove player.
function busHandler:AddPlayer(PlayersTable, Player)
local CurrentTable = PlayersTable
table.insert(CurrentTable, Player.Name)
return CurrentTable
end
function busHandler:RemovePlayer(PlayersTable, Player)
local CurrentTable = PlayersTable
local Check = table.find(CurrentTable, Player.Name)
if Check then
table.remove(CurrentTable, Check)
return CurrentTable
end
end
It seems I didn’t quite portray a better explanation of the problem. As I said I need to find a way to prevent players from Listing and Un-listing immediately. Since I want the players to get inside and had to touch the door again to get out.
this is my preferred method as I don’t use wait in script
local debounceTable = {}
local function coolDown(plr, second)
if debounceTable[plr.UserId] == nil then
debounceTable[plr.UserId] = os.time()
return true
else
if os.time() - debounceTable[plr.UserId] >= second then
debounceTable[plr.UserId] = os.time()
return true
else
return false
end
end
end
if coolDown(Player, 1) then
-- do staff here
else
print("please wait for cooldown")
end
I don’t see why it would cause any issues. delay is used for, as the name implies, to delay the call of the function provided given for the given amount of seconds, without interrupting the current thread.
But that doesn’t happen. A new thread is created when the function is called. It’ll still pause for a certain amount of time before setting the player within the player array to nil.
local playerList = {}
-- Function goes here but can stil be called even when waiting
if (not playerList[player] == player) then
playerList[player] = player
-- Code
wait(2) -- Will wait
playerList[player] = nil
end