evasivedocs
Roblox scriptingLuau 0.748 · 24 guides · 385 API entries
Examples

Join and leave alerts

A notification card whenever a player joins or leaves your server, and a log file with the time, the place and the player's user id. The log is in workspace\logs\players.txt.

The players already in the server when the script starts are written to the log once, so the file always says who was there.

Luau
local Players = game:GetService("Players")

local LOG = "logs/players.txt"
local known = {}

local function remember(player)
    known[player] = {
        display = player.DisplayName,
        name = player.Name,
        id = player.UserId,
    }
    return known[player]
end

local function log(event, who)
    local line = string.format("%s  place %d  %-6s %s (%s, id %d)\n",
        os.date("%Y-%m-%d %H:%M:%S"), game.PlaceId, event, who.display, who.name, who.id)
    appendfile(LOG, line)
end

for _, player in ipairs(Players:GetPlayers()) do
    log("here", remember(player))
end

Players.PlayerAdded:Connect(function(player)
    local who = remember(player)
    log("join", who)
    notify(who.display .. " joined", "Players", 4)
end)

Players.PlayerRemoving:Connect(function(player)
    local who = known[player]
    known[player] = nil
    if who then
        log("leave", who)
        notify(who.display .. " left", "Players", 4)
    end
end)

print("watching", #Players:GetPlayers(), "players")

The script keeps running because it holds two event connections. Stop it from the Scripts tab when you no longer want alerts.

Why it remembers names

evasive notices a departure on its next check, after the player has already left. By then the player object is gone from the game and reads as empty, with no name. So the script stores what it wants to print when the player arrives, and looks it up when they leave. The player object still works as the table key.

Notes

  • evasive checks for new and departed players ten times a second, so an alert can come up to a tenth of a second late.
  • os.date uses your PC's clock and time zone.
  • To get a ping on your phone, swap notify for an httppost to a webhook. See Files and HTTP.