How to detect how many times a player died in a time window

I have just finished most of my Death screen sequence script and as it have some animations for when the player respawns, I wanted it to have custom animations for when the player die too much too quickly

how would I go to make a script count how much times a player died in, let’s say, 5 minutes?
I thought in making it set to 0 every 5 minutes but I noticed it would be pretty ineffective, as you can die right at the last second of the 5 minutes and it reset instantly
or I can make a function that add 1 to a death count, use task.wait to wait for the 5 minutes then decrease by 1, but i think it would also be pretty unoptimized

1 Like

Hello, I made a script to detect how many time a player died in a time window.

Try this:

local deaths = {}

-- Function to track player deaths
local function trackDeath()
    table.insert(deaths, os.time()) -- Store current timestamp
end

-- Function to clean up old timestamps
local function cleanupDeaths()
    local currentTime = os.time()
    for i = #deaths, 1, -1 do
        if currentTime - deaths[i] > 300 then -- Checks if the timestamp is older than 5 minutes which is 300 seconds.
            table.remove(deaths, i)
        end
    end
end

-- Functtion to get number of deaths in the last 5 minutes
local function getDeathsInLastFiveMinutes()
    cleanupDeaths()
    return #deaths
end

-- Example
trackDeath()

-- Example of getting the number of deaths in the last 5 minutes
local deathsInLastFiveMinutes = getDeathsInLastFiveMinutes()
print("Deaths in the last 5 minutes: " .. deathsInLastFiveMinutes)

alright, I’m gonna test it later, but by just reading it right now it does seem like it works, just gonna do some modifications so it is usable server-sided but I can do it myself, when I test it I will tell how it goes

2 Likes

Hello, have you tested the script yet?

oh, sorry I forgot! I woke up kinda late and had some things to do, hopefully I remember when I get home

It’s okay! When you do contact me :grin:

alright! I’ll do it when I wake up, sorry for the wait! gonna set up a alarm so I don’t forget

1 Like

yup! its working, i made some modifications and this is how it turned out

local module = {}

local deaths = {}

function module.trackPlayer(Player : Player)
	deaths[Player] = {}
	Player.CharacterAdded:Connect(function(char :Model)
		print("starting the tracking of "..Player.Name)
		local human : Humanoid = char:WaitForChild("Humanoid")
		human.Died:Connect(function()
			module.trackDeath(Player)
			print(module.getDeathsInLastFiveMinutes(Player))
		end)
	end)
end

-- Function to track player deaths
function module.trackDeath(plr : Player)
	table.insert(deaths[plr], os.time()) -- Store current timestamp
end

-- Function to clean up old timestamps
function module.cleanupDeaths()
	local currentTime = os.time()
	for i,v in pairs(deaths) do
		for index,times in pairs(v) do
			if currentTime - times > 300 then
				table.remove(deaths[i], index)
			end
		end
	end
end

-- Functtion to get number of deaths in the last 5 minutes
function module.getDeathsInLastFiveMinutes(targetPlayer : Player)
	module.cleanupDeaths()
	return #deaths[targetPlayer]
end

return module

1 Like

That’s amazing, I’m happy I could help you. :grin:

1 Like