Advertisement
Rochet2

Run lua from ingame or console

Feb 25th, 2015
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. local runcmd = "eluna"
  2. local function RunCommand(event, player, cmd)
  3.     if (cmd:lower():find("^"..runcmd.." .+")) then
  4.         -- Here you can define some environment variables for the code
  5.         -- I defined plr to be the player or nil
  6.         -- sel to be the current selection of the player or nil
  7.         -- and print to be sendbroadcastmessage if the player is not nil
  8.         local env = [[
  9.             local plr = ...
  10.             local sel = sel
  11.             local pp = function(p, ...)
  12.                 local t = {...}
  13.                 for i = 1, select("#", ...) do t[i] = tostring(t[i]) end
  14.                 p:SendBroadcastMessage(table.concat(t, " "))
  15.             end
  16.             if (plr) then
  17.                 sel = plr:GetSelection()
  18.             end
  19.         ]]
  20.         local code = env..cmd:sub(#runcmd+2)
  21.         local func, err = load(code, "."..runcmd)
  22.         if (func) then
  23.             local res
  24.             res, err = pcall(func, player)
  25.             if (res) then
  26.                 return false
  27.             end
  28.         end
  29.         print(err)
  30.         if (not player) then
  31.             print(err)
  32.         else
  33.             player:SendBroadcastMessage(err)
  34.         end
  35.         return false
  36.     end
  37. end
  38.  
  39. RegisterPlayerEvent(42, RunCommand)
  40.  
  41. -- Example usage:
  42. -- Note that player doesnt exist if this is run from console, so I made it check for that. This command can be ran from console and ingame
  43. -- .eluna print(GetLuaEngine(), plr and plr:GetName() or "console")
  44. -- I defined pp to be a function to send a broadcast message to first passed player
  45. -- sel is the selection of plr
  46. -- .eluna pp(plr, plr:GetName(), sel and sel:GetName())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement