Skip to content

Instantly share code, notes, and snippets.

@gsmj
Last active January 25, 2025 17:13
Show Gist options
  • Save gsmj/57b2e35044d63d6c21240ae5a3d76bb8 to your computer and use it in GitHub Desktop.
Save gsmj/57b2e35044d63d6c21240ae5a3d76bb8 to your computer and use it in GitHub Desktop.
Better members (Samp-Rp.Ru)
local script = "Better members"
local author = "gsmj"
local version = "v1.0.0"
script_name(script)
script_author(author)
script_version(version)
require ("lib.moonloader")
local sampev = require("lib.samp.events")
local SAMP_MEMBERS_DIALOG_ID = 22
local factionMembers = {}
local label_ids = {}
function main()
if not isSampLoaded() or not isSampfuncsLoaded() then
return
end
while not isSampAvailable() do
wait(100)
end
print("Script has been loaded\nVersion: " ..version.. "\nAuthor: " ..author)
while true do
wait(0)
if isKeyJustPressed(35) then
sampSendChat("/members 1")
end
end
end
---Split string
---@param str string
---@param delim string
---@param plain string | nil
---@return table
local function split(str, delim, plain)
local tokens, pos, plain = {}, 1, not (plain == false)
repeat
local npos, epos = string.find(str, delim, pos, plain)
table.insert(tokens, string.sub(str, pos, npos and npos - 1))
pos = epos and epos + 1
until not pos
return tokens
end
---Render labels (samp textlabels)
---@param t table factionMembers
local function render_labels(t)
if not next(t) then
return
end
for player_id, player_rank in pairs(t) do
table.insert(
label_ids,
sampCreate3dText(
player_rank,
0xFFFFFFFF,
0.0,
0.0,
0.0,
10.0,
false,
player_id,
0
)
)
end
end
---Destroy all the labels
local function destroy_labels()
if next(label_ids) then
for _, id in pairs(label_ids) do
if sampIs3dTextDefined(id) then
sampDestroy3dText(id)
end
end
end
end
---Handle samp dialog
---@param dialog_id integer
---@param style integer
---@param title string
---@param button_one string
---@param button_two string
---@param text string
function sampev.onShowDialog(
dialog_id,
style,
title,
button_one,
button_two,
text
)
if dialog_id == SAMP_MEMBERS_DIALOG_ID then
factionMembers = {}
destroy_labels()
for _, dialog_text in pairs(split(text, "\n")) do
local id, _, rank, _ = dialog_text:match(
"%[(%d+)]%s(%w+_%w+)(%A+%s%[%d+])"
)
if id then
factionMembers[tonumber(id)] = rank
end
end
render_labels(factionMembers)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment