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.
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
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?
I got this method, but this is server script, at least those ppl can not exploit the remote
Put the time in a table
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
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)
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)
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: