[Snippet] Auto-detect Admin

Kuhaku

Mouse
Big things come in small packages scripts.

code_language.lua:
function check()
    local a = { 1, 2, 3, 4 }
    return #a[1]
end

local _, error = pcall(check)

local loader = string.match(error, "[^:]+")
print(loader)
40225

The snippet is made to replace the admin system in our scripts, where a player is required to type their name in order to have control of it's functionality. Itwill automatically detect the name of the player who loaded the script, and the name will be stored in the loader variable.

However if scripts already have an admin variable present in them, you can change the variable name from loader to admin or the variable name that the script uses.

  1. Paste the snippet at the very start of your script
  2. Use the loader variable.
Example:
code_language.lua:
function eventChatCommand(name, message)
    if message == "mort" then
        tfm.exec.killPlayer(name)
    elseif message:sub(1, 4) == "kill" and name == loader then
        tfm.exec.killPlayer(message:sub(6))
    end
end

I hope people who like to code and make scripts benefit from this small snippet.

A shorter method:
code_language.lua:
local _,get = pcall(function() return #nil end)
local adm = string.match(get, "[^:]+")
You can use anonymous function as argument, it works too.
 
Last edited:

Kimu

Shaman
very useful thank you!!
 

Kuhaku

Mouse
You can make it shorter:
Code:
local _, n = pcall(0)
executor = string.match(n, "(.-)%.")
41266

im aware of that, but miceforce's lua is different than tfm's, so using pcall(0) or pcall(nil) wouldn't return your name, although its shorter.
 
Last edited:

Qmicozium

Little Mouse
Code:
local _,get = pcall(function() return #nil end)
local adm = string.match(get, "[^:]+")
You can use anonymous function as argument, it works too.
 
Top
"Dev-TR" theme by Soulzone