Minute Tracker Fix

Hey, everyone,

I have a minute tracker that the aim is to count a players activity from when they join a game to when they leave a game and post it to discord.

The error is it seems to posting the time since the server opened, instead of the time they have been in the game.

local JoinTime = tick()

function PlayerLeaving(Player)

	local elapsedtime = tick() - JoinTime
	local minutes = math.floor(elapsedtime / 60)
	local points = math.floor(minutes / 10)

	local HS = game:GetService("HttpService")
	local Webhook = "" --removed for privacy
	local BOT = "" .. Player.Name .. "/" .. minutes --removed for privacy

	local payload = HS:JSONEncode({
		content = "**NEW ACTIVITY LOG** \n **Username:** "..Player.Name.." \n **Rank:** "..Player:GetRoleInGroup(9266615).."  \n **Game:** Candify Bakery \n **Time:** " ..minutes.." minute(s) \n **Points:** "..points,
		username = "Activity Logger"
	})

	HS:PostAsync(Webhook, payload)
	HS:GetAsync(BOT)
	
end

game.Players.PlayerRemoving:Connect(function(Player)
	PlayerLeaving(Player)
end)

All support is appreciated!

Well if this is in a server script, you’ll have to listen to PlayerAdded and save a dictionary of players to join times. I wouldn’t do this on the client because it’s being done through a webhook, and you don’t want an exploiter getting their hands on that.

On player connect you can do something like JoinTimes[Player] = tick() given JoinTimes is a table {} then on remove, tick() - JoinTimes[Player] Then set JoinTimes[Player] to nil to free the memory.

2 Likes

That makes sense cause ‘JoinTime’ is defined when the script runs for the first time (which is after the server has started).

Do you mind adding this to the script provided, I’m quite lost. :sweat_smile:

I already gave you the relevant snippets you need in my original reply. JoinTimes should be a table ({}), on PlayerAdded, set JoinTimes[Player] = tick(), on remove the same logic you already have applies tick() - JoinTimes[Player]. After you’re done with it set JoinTimes[Player] = nil so you don’t use memory unnecessarily.

This is a simplified implementation of @steven4547466’s idea:

local Players = game:GetService("Players")

local joinTimes = {}

Players.PlayerAdded:Connect(function(player)
	joinTimes[player.Name] = tick()
end)

Players.PlayerRemoving:Connect(function(player)
	local playTime = tick() - joinTimes[player.Name]
	joinTimes[player.Name] = nil
end)

This is the script I have created - is this correct?

local JoinTime = {}

game.Players.PlayerAdded:Connect(function(Player)
	JoinTime[Player.Name] = tick()
end)

function PlayerLeaving(Player)

	local elapsedtime = tick() - JoinTime[Player.Name]
	local minutes = math.floor(elapsedtime / 60)
	local points = math.floor(minutes / 10)

	local HS = game:GetService("HttpService")
	local Webhook = ""
	local BOT = "
" .. Player.Name .. "/" .. minutes

	local payload = HS:JSONEncode({
		content = "**NEW ACTIVITY LOG** \n **Username:** "..Player.Name.." \n **Rank:** "..Player:GetRoleInGroup(9266615).."  \n **Game:** Candify Bakery \n **Time:** " ..minutes.." minute(s) \n **Points:** "..points,
		username = "Activity Logger"
	})

	HS:PostAsync(Webhook, payload)
	HS:GetAsync(BOT)
	
	JoinTime[Player] = nil
end

game.Players.PlayerRemoving:Connect(function(Player)
	PlayerLeaving(Player)
end)

Should be JoinTime[Player.Name] = nil.

1 Like