Kill Counter That Expires

I’m not sure how I can make a kill counter that expires after a certain amount of time. The count will be represented through a textlabel.

I know how to make the kills count, but I’m not sure how to make the kill count reset after a certain amount of time.

When is the “expiration timer” supposed to begin?

I suggest doing it like this:

local Timer = (Number)
local Kills = (Number)
local Waited = false

-- Called when a player is killed...
-- Most credits to @Maelstorm_1973
local function playerKilled()
	Kills += 1
	if waited == false then
		waited = eventWait(eliminations.Changed, timeout)
	end
end

Edit: Forgot to add the part to make the counter and function it :sweat_smile:

Unclear to where this code should be placed in.

If the kills are meant to reset after a certain amount of time has passed after the last kill (sorta like a Combo counter), then I’d suggest having a structure like this:

  • Kill Received Event Fires:
    • If variable expirationThread exists
      • task.cancel(expirationThread)
      • expirationThread = nil
    • Do existing logic…
    • Spawn a new thread with task.delay(expireTime, func)
      • Keep track of this thread (assign the expirationThread variable to the result of the call)
      • func should be a function which resets the kill counter

The thread cancellation logic is to make sure that the kills don’t unintentionally get reset when a new kill is received.

2 Likes

Read this topic first, then use this code:

local eliminations = Instance.new("IntValue")
eliminations.Value = 0
local waited = false
local timeout = 10


--
-- Whatever code you're doing...
--

-- Called when a player is killed...
local function playerKilled()
	local value = eliminations.Value
	value += 1
	eliminations.Value = value
	if waited == false then
		waited = eventWait(eliminations.Changed, timeout)
	end
end
1 Like

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