How to keep track of time played?

Hello, I am trying to create a script that will log how long each player has played and every 15 minutes a player has played a message will be sent to discord.

The issue is I am a builder, and have very low experience in scripting. I have researched how to use webhooks and such, and playeradded events. But I don’t know how to track time for each individual in the game. If someone can help me out I would greatly appreciate it.

1 Like

you could simply setup a while loop to wait for amount of time in seconds then call function to contact discord

local waittime = 900 -- seconds to wait before sending   900 seconds equals your 15 mins  but for testing may want to make it less like 15 or something

function contactdiscord()

--- code to send to discord

end

while true do

wait(waittime)

print('played ' .. waittime)

end
2 Likes

No idea if this method is 100% what OP is asking for, but I have a slight correction regardless. waittime would be 900, not 15.

1 Like

Would this send the name of every player in game? For instance if someone joined 40 seconds before the 15 minutes was up. Would their name be included?

If you run this in a local script from say StartGui it will then just run the contactdiscord() function after the lapsed time for that player only

Be careful with this though. Having a remote that has no parameters to verify could be abused by exploiters and lead to API abuse.

1 Like

Okay, so place a local script with the while true do, then it will work for that player only?

correct but like he said above you need a way to check that the remote fired is valid with a parameter sent to server

I really wish I knew what that meant.

I’ll figure that out lol. Thanks yall

I got this method, but this is server script, at least those ppl can not exploit the remote

  1. Put the time in a table
  2. Send them after 15 mins they join
local TimePlayed = {}
game.Player.PlayerAdded:Connect(function(plr)
    local TimePlayed[#plr] = tick() --tick() means the time in server, you can use os.time if u want
    local backUptime = TimePlayed[#plr] 
    wait(60 * 15) --wait 15 mins
    
    if backUptime ~= TimePlayed[#plr] then
        return 0
    end

    local timeDiff = tick() - TimePlayed[#plr]  --The time played in this server
    
    SendToDiscord(plr.Name, timeDiff) 

end)

Something you should consider

  1. What if the player joined 2+ times in 15 mins
local TimePlayed = {}
game.Player.PlayerAdded:Connect(function(plr)
    if TimePlayed[#plr] then --send the time immediately if they join less then 15 mins 
        SendToDiscord(plr.Name, timeDiff) 
    end
    local TimePlayed[#plr] = tick()
    wait(60 * 15)
    local timeDiff = tick() - TimePlayed[#plr]  --The time played in this server
    TimePlayed[#plr] = nil --To let the server know it is sent
    
    SendToDiscord(plr.Name, timeDiff) 

end)
1 Like

Here is a server script that will set a timestamp then go through everyseconds and check to see if the playertime is equal or greater than the wait time then fires the discord function

If you use this your wont have to bother with remote functions or anything
Just put this in a serverscript in ServerScriptService

local timeTrack = {}
local waittime = 900 -- seconds to wait before sending   900 seconds equals your 15 mins  but for testing may want to make it less like 15 or something

function contactdiscord(player)

	--- code to send to discord

end

function PlayerAdded(player)
	-- Take note of when the player started playing:
	timeTrack[player] = tick()
end

function PlayerRemoving(player)   -- removes that player from table
	timeTrack[player] = nil
end

game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoving)

while true do
	for k, v in pairs(timeTrack) do   -- k is the player   v is the timestamp or tick
		local start = v  -- gets the last timestamp
		local timePlayed = tick() - start   -- gets the seconds from last timestamp for this player
		if timePlayed >= waittime then   -- if the timeplayed is equal or greater than the set waittime
			contactdiscord(k)   -- calls discord function above and sends the player over
			timeTrack[k]  = tick()  -- this sets the new timestamp
		end
	end
	wait(1)
end

Opps I found that my script I coded is wrong, use this instead

local TimePlayed = {}
game.Players.PlayerAdded:Connect(function(plr)
    local TimePlayed[#plr] = tick() --tick() means the time in server, you can use os.time if u want
    local backUptime = TimePlayed[#plr] 
    wait(60 * 15) --wait 15 mins
    
    if backUptime ~= TimePlayed[#plr] then
        return 0
    end

    local timeDiff = tick() - TimePlayed[#plr]  --The time played in this server
    
    SendToDiscord(plr.Name, timeDiff) 
    TimePlayed[#plr] = nil

end)
game.Players.PlayerRemoving:Connect(function(plr)
    if not TimePlayed[#plr] then
        return 
    end
    local timeDiff = tick() - TimePlayed[#plr]  --The time played in this server
    SendToDiscord(plr.Name, timeDiff) 
end)
1 Like

Not only does that code not properly answer OP’s question (which is to keep track of time played and not to run a function after every 15 minutes), but now you have an improperly written while loop on your hands and blocked threads, not lag. Keep in mind that Lua isn’t multi-threaded.

In regards to the improperly written while loop, here’s an article I love sharing around when I see them:

1 Like