Skip to content

Instantly share code, notes, and snippets.

@ToeKneeRED
Last active August 9, 2024 04:32
Show Gist options
  • Save ToeKneeRED/e5ba2d2f136e1198b8fdf39c0f30a477 to your computer and use it in GitHub Desktop.
Save ToeKneeRED/e5ba2d2f136e1198b8fdf39c0f30a477 to your computer and use it in GitHub Desktop.
JoinLeaveMessages STR plugin
print("[JoinLeaveMessages] Started plugin")
local playerMgr = PlayerManager:get()
local gameServer = GameServer:get()
local entList = {}
local playerCount = 0
function getPlayer(entityId)
local players = playerMgr:GetAllPlayers()
for k, player in pairs(players) do
if player:GetCharacter() == entityId then
return player
end
end
return nil
end
addEventHandler("onCharacterSpawn", function(entityId)
local player = getPlayer(entityId)
if not player then
return
end
if entList[entityId] == nil then
entList[entityId] = player
playerCount = playerCount + 1
print("[JoinLeaveMessages] " .. player:GetUsername() .. " joined the server")
local players = playerMgr:GetAllPlayers()
for _, p in pairs(players) do
if p:GetCharacter() ~= entityId then
gameServer:SendChatMessage(p:GetConnectionId(), player:GetUsername() .. " joined the server")
else
local playerNames = ""
local first = true
for _, pl in pairs(players) do
if first then
playerNames = pl:GetUsername()
first = false
else
playerNames = playerNames .. ", " .. pl:GetUsername()
end
end
gameServer:SendChatMessage(p:GetConnectionId(), "Players online (" .. playerCount .. "): " .. playerNames)
end
end
end
end)
addEventHandler("onCharacterDestroy", function(entityId)
local player = getPlayer(entityId)
if not player then
return
end
print("[JoinLeaveMessages] Player " .. player:GetUsername() .. " left the server")
entList[entityId] = nil
playerCount = playerCount - 1
for _, p in pairs(entList) do
if p:GetCharacter() ~= entityId then
gameServer:SendChatMessage(p:GetConnectionId(), player:GetUsername() .. " left the server")
end
end
end)
[Resource]
name = "Join/Leave Messages"
version = 1.0.0
apiset = 1.0.0
description = "Sends user join and leave messages to chat and server"
keywords = ["example", "resource"]
license = "MIT"
repository = ""
entrypoint = "joinleavemessages.lua"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment