Can someone explain this code to me

So I used AI to generate this code for me bc my method was too complicated and would take up too many variables and lines but pretty what I am trying to achieve is that if the player is killed 5 times within 30 seconds then they will lose their admin perms (I am making a kohl’s admin house remake)


local kickThreshold = 5  -- Number of deaths required for kick
local timeThreshold = 30  -- Time window in seconds
local kickMessage = "You've been kicked for dying too many times within a short period."

local deathCount = {}  -- Table to store death counts for each player

-- Function to handle player death
local function onPlayerDeath(player)
    local playerId = player.UserId
    -- Initialize death count if not already present
    deathCount[playerId] = deathCount[playerId] or { count = 0, lastDeathTime = tick() }
    -- Check if the last death occurred within the time threshold
    local currentTime = tick()
    if currentTime - deathCount[playerId].lastDeathTime <= timeThreshold then
        -- Increment death count
        deathCount[playerId].count = deathCount[playerId].count + 1
        -- Check if death count exceeds threshold
        if deathCount[playerId].count >= kickThreshold then
            -- Kick player
        -- Reset death count
        deathCount[playerId].count = 1
    -- Update last death time
    deathCount[playerId].lastDeathTime = currentTime

-- Connect the function to the player death event

  1. Variables:
  • kickThreshold is set to 5, which is the number of deaths required for a player to be kicked.
  • timeThreshold is set to 30 seconds, which is the time window within which the deaths are counted.
  • kickMessage is the message displayed to the player when they are kicked for dying too many times.
  1. deathCount Table:
  • This table stores the death counts for each player along with the timestamp of their last death.
  1. onPlayerDeath Function:
  • This function is called whenever a player dies.
  • It takes the player object as an argument / parameter
  • It checks if the player’s death count exceeds the threshold within the time frame.
  • If the player exceeds the threshold, they are kicked from the game, with the kickMessage attached.
  • Otherwise, it increments the death count for the player or resets it if the time window has passed since their last death.
  1. Player Connection:
  • The script connects the onPlayerDeath function to the Died event of each player’s humanoid character.
  • This connection ensures that the onPlayerDeath function is triggered whenever a player’s character dies.
  • This part connects the onPlayerDeath function to the event of a new player joining the game.
  1. Player Added Connection:
  • It makes sure that whenever a new player joins, their character’s death is monitored.
1 Like

Ok now my question is how would I remove the player’s key from the deathCount table. The reason I want to do this is bc incase the player gets killed 5 times again within 30 seconds

deathCount[playerId] = nil

Or you can do

table.remove(deathCount, table.find(deathCount, playerId))

already tried the lower one but didnt work but maybe bc I put it in the wrong place

No it won’t work because table.remove/table.find don’t play nice with tables that don’t strictly use table.insert.

1 Like