PDA

View Full Version : Useful functions


Boylett
03-29-2009, 06:07 AM
Post your useful functions here!

Every MP needs this topic :D I'll start us off:

function outputExcept(PlayerID,message,r,g,b,a)
for i=0,99,1 do
if PlayerID ~= i and isPlayerConnected(i) == 1 then
outputChatBox(i,message,r,g,b,a)
end
end
end

That sends a message to everyone in the server, except PlayerID

Relax
03-29-2009, 06:40 AM
To give weapon to the player
function GiveWeapon(PlayerID,WeaponID,Ammunition)
if WeaponID == nil or Ammunition == nil then outputChatBox(PlayerID,">> Syntax: /weapon <weaponid> <ammo-count>",255,0,0,255) return end
wid = strval(WeaponID)
amm = strval(Ammunition)
givePlayerWeapon(PlayerID,wid,amm)
end
addCommand("weapon","GiveWeapon")

Ghost Dog 13
03-29-2009, 07:18 AM
To put Drunk Move on :P


function drunk(PlayerID, Enabled)
if Enabled == nil then return end
togglePlayerDrunk ( PlayerID,Enabled )
end
addCommand("drunk","drunk")

Relax
03-29-2009, 12:22 PM
To overturn vehicle to the wheels if it overturned to the roof
function VehFlip(PlayerID,VehicleID)
if isPlayerInAnyVehicle(PlayerID) == 1 then
local X, Y, Z = getPlayerPos(PlayerID)
VehicleID = getPlayerVehicleID(PlayerID)
setVehiclePos(VehicleID,X,Y,Z)
else outputChatBox(PlayerID,"You must be in vehicle!",255,0,0,255) return end
end
addCommand("flip","VehFlip")

Ghost Dog 13
03-29-2009, 12:54 PM
wow thanks :)

I was looking for this command!

Relax
03-29-2009, 01:14 PM
change vehicle color
function VehCol(VehicleID, Color_1, Color_2)
if isPlayerInAnyVehicle(PlayerID) == 1 then
if Color_1 == nil or Color_2 == nil then outputChatBox(PlayerID,">> Syntax: /vehcol <color 1[0-255]> <color 2[0-255]>",255,0,0,255) return end
color1 = strval(Color_1)
color2 = strval(Color_2)
local vehid = getPlayerVehicleID (PlayerID)
setVehicleColor ( vehid, color1, color2 )
else outputChatBox(PlayerID,"You must be in vehicle!",255,0,0,255) return end
end
addCommand("vehcol","VehCol")

Ghost Dog 13
03-29-2009, 01:42 PM
Can somebody give me 1 Function to repair the car?

Commands = "rp" and "repair"...

[LCK]G0LdzEr0
03-29-2009, 01:46 PM
Script by Relax

function RepairVehicle(PlayerID,VehicleID)
if isPlayerInAnyVehicle(PlayerID) == 1 then
VehicleID = getPlayerVehicleID(PlayerID)
repairVehicle(VehicleID)
outputChatBox(PlayerID,"You repair vehicle!",255,0,0,255)
else outputChatBox(PlayerID,"You must be in vehicle!",255,0,0,255) return end
end
addCommand("repair","RepairVehicle")

Relax
03-29-2009, 01:47 PM
give weapon for some player
function GiveWeaponForPlayer(PlayerID,TargetID,WeaponID,Amm unition)
senderid = strval(TargetID)
wid = strval(WeaponID)
amm = strval(Ammunition)
if WeaponID == nil or Ammunition == nil or TargetID == nil then outputChatBox(PlayerID,">> Syntax: /gw <playerid> <weaponid> <ammo-count>",255,0,0,255) return end
if isPlayerConnected(TargetID) == 0 then outputChatBox(PlayerID,"Error: That player is not connected.",255,0,0,255) return end
givePlayerWeapon(senderid,wid,amm)
end
addCommand("gw","GiveWeaponForPlayer")

Ghost Dog 13
03-29-2009, 02:16 PM
Thanks :)

Mantis
03-29-2009, 02:41 PM
Toggle Car Lights On/Off

function ToggleCarLights(PlayerID, Status)

if Status == nil then
outputChatBox(PlayerID,">> Syntax: /lights on/off",255,0,0,255)
return
end

if Status == "on" then
toggleVehicleLights(getPlayerVehicleID(PlayerID), 1)
end

if Status == "off" then
toggleVehicleLights(getPlayerVehicleID(PlayerID), 0)
end
end

addCommand("lights","ToggleCarLights")

Ghost Dog 13
03-29-2009, 03:01 PM
Hmm i'll make this function by pressing "L" in keyboard :)

Edit:

Nah.. Can't do it :/

Relax
03-29-2009, 06:13 PM
This function works if on your server it is established script VCO: Admin (http://www.gtagaming.com/forums/showthread.php?t= 114484)

GameText Admin Message
function AdminCnnMsg(PlayerID,OutText)
if IsPlayerAdmin[PlayerID] == 0 then
outputChatBox(PlayerID," you are not authorized to use that command!",255,0,0,255) return end
if OutText == nil then
outputChatBox(PlayerID,"USAGE: /cnn [text]",255,0,0,255) return end
local Color = "~t~"
gameTextForAll(Color .. getPlayerName(PlayerID) .. ": ".. OutText,3,1500)
end
addCommand("cnn","AdminCnnMsg")

XAOC
03-30-2009, 12:57 AM
Function String crypt by the key
function CryptByKey(String,Key)
local lenstr = string.len(String)
local retstr = ""
for i = 1, lenstr do
local retrncrypt = strval(String:byte(i))+strval(Key:byte(i))
retstr = string.format('%s%c',retstr,retrncrypt)
end
return retstr
end

Example use:
print(CryptByKey("i crypted string","i key"))

---------------------------------------------------------------------------

function GiveDec(Hex)
if(Hex == "A")then ValNum = 10;
elseif(Hex == "B")then ValNum = 11;
elseif(Hex == "C")then ValNum = 12;
elseif(Hex == "D")then ValNum = 13;
elseif(Hex == "E")then ValNum = 14;
elseif(Hex == "F")then ValNum = 15;
else ValNum = strval(Hex)
end
return ValNum;
end

function SendPlayerMessage(PlayerID,Message,Color)

local a = GiveDec(string.format('%c',Color:byte(1)))
local b = GiveDec(string.format('%c',Color:byte(2)))
local c = GiveDec(string.format('%c',Color:byte(3)))
local d = GiveDec(string.format('%c',Color:byte(4)))
local e = GiveDec(string.format('%c',Color:byte(5)))
local f = GiveDec(string.format('%c',Color:byte(6)))

local R = (a * 16) + b;
local G = (c * 16) + d;
local B = (e * 16) + f;
outputChatBox(strval(PlayerID),Message,strval(R),s trval(G),strval(B),255)
end
Example use:
SendPlayerMessage(PlayerID,"This a hex color message",'FFFFFF')

;)

-----------------------------------------------------

function GetChar(String,CharNum)
return string.char(String:byte(strval(CharNum)))
end
Example use:
GetChar("string",4) -- returned 'i'

Ghost Dog 13
03-30-2009, 06:04 AM
WOW!

Awesome Mate :)

Ettans
03-30-2009, 08:39 AM
An example on how to use arrays in LUA:


-- An example array. Note: Automatic indexing in LUA starts at 1
-- The name of the array
Array =
{
a = "Value 1", -- 'a' as input will print "Value 1"
b = "Value 2", -- 'b' as input will print "Value 2"
c = "Value 3", -- 'c' as input will print "Value 3"
d = "Value 4", -- 'd' as input will print "Value 4"
e = "Value 5" -- 'e' as input will print "Value 5"
}
-- Array test command. Syntax: '/arraytest <value>'
-- If you use '/arraytest a', it will print "Value 1" in the server's console
function ArrayTest(PlayerID,Val)
print(Array[Val])
end
addCommand("arraytest","ArrayTest")


Clears the VCO chat screen for all connected players (Type '/clear' to clear it):


function ClearChat(PlayerID)
for i = 0, 100 do
if isPlayerConnected(i) == 1
for x = 0, 8 do
outputChatBox(i," ",255,255,255,255)
end
end
end
end
addCommand("clear","ClearChat")

Killerkid
03-30-2009, 10:55 AM
Hmm i'll make this function by pressing "L" in keyboard :)

Edit:

Nah.. Can't do it :/

local lightstatus = {}

function onPlayerConnect(PlayerID)
lightstatus[PlayerID] = 0
end

function onPlayerKeyPress(PlayerID, key)
if key == 76 then
if isPlayerInAnyVehicle(PlayerID) == 1 then
if lightstatus[PlayerID] == 0 then
toggleVehicleLights(getPlayerVehicleID(PlayerID), 1)
lightstatus[PlayerID] = 1
return
end
if lightstatus[PlayerID] == 1 then
toggleVehicleLights(getPlayerVehicleID(PlayerID), 0)
lightstatus[PlayerID] = 0
return
end
end
end
end

Toiletduck
03-30-2009, 01:38 PM
function Freeze(PlayerID,TargetID)
if isPlayerConnected(TargetID) == 0 then outputChatBox(PlayerID,"Error: That player is not connected.",255,0,0,255) return end
if TargetID == nil or Text == nil then outputChatBox(PlayerID,"Error: /freeze <id>",255,0,0,255) return end
if strval(PlayerID) == strval(TargetID) then outputChatBox(PlayerID,"Error: You cannot freeze yourself.",255,0,0,255) return end

togglePlayerControllable(TargetID,0)
outputChatBox(PlayerID,getPlayerName(TargetID).." was frozen by :"..getPlayerName(PlayerID)..".",255,255,255,255)
end

addCommand("freeze","Freeze")


function Thaw(PlayerID,TargetID)
if isPlayerConnected(TargetID) == 0 then outputChatBox(PlayerID,"Error: That player is not connected.",255,0,0,255) return end
if TargetID == nil or Text == nil then outputChatBox(PlayerID,"Error: /thaw <id>",255,0,0,255) return end
if strval(PlayerID) == strval(TargetID) then outputChatBox(PlayerID,"Error: You cannot freeze yourself.",255,0,0,255) return end

togglePlayerControllable(TargetID,1)
outputChatBox(PlayerID,getPlayerName(TargetID).." was thawed by :"..getPlayerName(PlayerID)..".",255,255,255,255)
end

addCommand("thaw","Thaw")

Boylett
03-30-2009, 02:01 PM
chr(number)

This convert a number (e.g. ascii) into it's character

function chr(number)
return string.format("%c",number)
end

To reverse this, you can use string.ascii(s,[i]) (which is built into Lua)

Toiletduck
03-30-2009, 02:08 PM
chr(number)

This convert a number (e.g. ascii) into it's character

function chr(number)
return string.format("%c",number)
end

To reverse this, you can use string.ascii(s,[i]) (which is built into Lua)


What is this used for?

XAOC
03-30-2009, 03:33 PM
chr(number)

This convert a number (e.g. ascii) into it's character

function chr(number)
return string.format("%c",number)
end

To reverse this, you can use string.ascii(s,[i]) (which is built into Lua)


Use

string.char(numbers...)

;)

StarScream
03-31-2009, 12:15 PM
Toggle Icon Flashing. (If it's wrong meh..i'm just learning)


function SetIconFlashing(IconID, State)

if State == nil then
outputChatBox(PlayerID,">> Syntax: /iconflash [on/off]",255,0,0,255)
return
end

if State == "on" then
toggleGlobalIconFlashing(IconID, 1)
end

if State == "off" then
toggleGlobalIconFlashing(IconID, 0)
end
end

addCommand("iconflash","toggleiconflash")

Mex
03-31-2009, 01:11 PM
Toggle Icon Flashing. (If it's wrong meh..i'm just learning)


function SetIconFlashing(IconID, State)

if State == nil then
outputChatBox(PlayerID,">> Syntax: /iconflash [on/off]",255,0,0,255)
return
end

if State == "on" then
toggleGlobalIconFlashing(IconID, 1)
end

if State == "off" then
toggleGlobalIconFlashing(IconID, 0)
end
end

addCommand("iconflash","toggleiconflash")

yo, looks fine to me, although the second paramter that you send to addCommand() should be the function name

addCommand("iconflash","SetIconFlashing")

StarScream
04-01-2009, 10:52 AM
Small Command Add-on to Ettans Admin System - /disarm


function DisarmPlayer(PlayerID,TargetID)
if IsPlayerAdmin[PlayerID] == 0 then
outputChatBox(PlayerID,"Error: You're not a server administrator.",255,0,0,255) return end
if TargetID == nil then
outputChatBox(PlayerID,"Syntax: /disarm <playerid>",255,0,0,255) return end
if isPlayerConnected(TargetID) == 0 then
outputChatBox(PlayerID,"Error: That player is not connected.",255,0,0,255) return end

clearPlayerWeapons(TargetID)
end
addCommand("disarm","DisarmPlayer")

Ettans
04-08-2009, 04:33 AM
Function: ReturnPlayerTeam(PlayerID)
Returns: Players current teams name

Code:

function ReturnPlayerTeam(PlayerID)
for i = 160,171 do
if getPlayerSkin(PlayerID) == i then return "TEAM_TOMMYS"
end
end
for i = 63,64 do
if getPlayerSkin(PlayerID) == i then return "TEAM_GOLFERS"
end
end
end


Example usage:

local ClassID = addSpawnClass("Tommy Boys",255,255,255,-924.072632,1050.113281,13.200515,180.0,-924.058716,1046.070435,11.8,8)
for i = 160, 171 do
addClassMember(ClassID,i,-975.841736,-353.321747,13.381999,179.083511,0,0,0,0,0,0)
end

function onPlayerSpawn(PlayerID)
if ReturnPlayerTeam(PlayerID) == "TEAM_TOMMYS" then
setPlayerColor(PlayerID,255,255,255)
end
end


On player spawn, checks if the team name is equal to "TEAM_TOMMYS", if yes, set the players color to white (255,255,255). If you need any more explanations, just say.

Cachibache
04-09-2009, 02:47 AM
Any command for change player color?

Knucis
04-09-2009, 06:52 AM
Any command for change player color?
Correct me if i am wrong:

function ChangeColor(PlayerID)
if Target == nil or Color1 == nil or Color 2 == nil or Color3 == nil then
outputChatBox(PlayerID, "Error - Missing information, /playercol <PlayerID> <Red> <Green> <Blue>",255,0,0,255) return end
setPlayerColor(Target,Color1,Color2,Color3)
end
addCommand("playercol","ChangeColor")

Mantis
04-09-2009, 02:07 PM
function ChangeColor(PlayerID,TargetID)
if TargetID == nil or Color1 == nil or Color 2 == nil or Color3 == nil then
outputChatBox(PlayerID, "Error - Missing information, /playercol <PlayerID> <Red> <Green> <Blue>",255,0,0,255) return end
setPlayerColor(TargetID,Color1,Color2,Color3)
end
addCommand("playercol","ChangeColor")

I think thats right, you missed TargetID (Might be wrong though!)

Xeonon
04-10-2009, 12:15 AM
Hmm i'll make this function by pressing "L" in keyboard :)

Edit:

Nah.. Can't do it :/

Sure enough you can if you just add a /key command :)

Mantis
04-10-2009, 06:57 AM
Sure enough you can if you just add a /key command :)

A working command has been posted that works for key press.

I think this topic is becoming a lil spammy, I think you should only post if you're posting code snippets that'll be useful for others. Asking for help should be posted elsewhere.

Xeonon
04-10-2009, 10:19 AM
No, Im just saying he could do it by creating a /key command, Im helping him. Not myself I already have a key command....

Inon
04-11-2009, 04:07 PM
function onPlayerKeyPress(PlayerID, KeyID)
if KeyID == 17 then -- 17 = Control key.
if getPlayerCellPhoneStatus ( PlayerID ) == 1 then
setPlayerCellPhoneStatus (playerID, 0)
else
setPlayerCellPhoneStatus (playerID, 1)
end
end
end

Warr1or
04-11-2009, 04:24 PM
sorry, who knows all the key codes? (cntrl = 17 etc)

Ettans
04-11-2009, 05:25 PM
Try this website: http://livedocs.adobe.com/flash/8/main/wwhelp/wwhi mpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001686.html

Maby it helps.

ReGeX
05-23-2009, 11:24 PM
Requested by Bruno:

function get2DDistanceBetweenPlayers(Source, Target)
if isPlayerConnected(Source) == 0 or isPlayerConnected(Target) == 0 then return 1E9 end
local X1, Y1, Z1 = getPlayerPos(Source)
local X2, Y2, Z2 = getPlayerPos(Target)
return math.sqrt(math.pow((X1 - X2),2.0) + math.pow((Y1 - Y2),2.0))
end


function get3DDistanceBetweenPlayers(Source, Target)
if isPlayerConnected(Source) == 0 or isPlayerConnected(Target) == 0 then return 1E9 end
local X1, Y1, Z1 = getPlayerPos(Source)
local X2, Y2, Z2 = getPlayerPos(Target)
return math.sqrt(math.pow((X1 - X2),2.0) + math.pow((Y1 - Y2),2.0)+ math.pow((Z1 - Z2),2.0))
end

Boylett
05-24-2009, 03:34 AM
You know, it's faster to just multiply the variables together than use math.pow, because that uses a complicated algorithm. Also, you can avoid square rooting if you square the distance you want to check :) unless you're using the distance for something else.

ReGeX
05-25-2009, 05:10 PM
Boylett - I don't think that an extra millisecond or two of processing will really make a difference, considering that it takes at least 10ms for the server to contact a client regardless. ;) Besides, the above method is more conventional.

[NoN]Bruno
05-25-2009, 08:58 PM
ReGeX : Hit 1 Hit 2 Hit 3 Hit 4 Hit 5 COMBO! ,

ReGeX Killed Boylett (Lua Scripting)

mr.tamplate
05-31-2009, 12:02 PM
hye guys im new in vco
and i want to ask you how to do when ppl do /commands they will see the commands that i added

Mex
05-31-2009, 01:05 PM
hye guys im new in vco
and i want to ask you how to do when ppl do /commands they will see the commands that i added

Welcome to vco :)

Well currently there's no way to get a list of all the commands that have been added using addCommand()

However this is no problem at all, you could simply do:

function view_commands(PlayerID)
local Commands = "/hp /car /test /etc"
outputChatBox(PlayerID, Commands, 20, 255, 20, 255)
end
addCommand("commands", "view_commands")

or a slightly more advanced way:

function view_commands(PlayerID)
local Commands = { "hp", "car", "test", "etc" }
local Commands2
for i,v in pairs(Commands) do
if Commands2 then Commands2 = Commands2 .. ", /" .. v
else Commands2 = "/" .. v
end
end
outputChatBox(PlayerID, Commands2, 20, 255, 20, 255)
end
addCommand("commands", "view_commands")

ZaR
06-02-2009, 06:09 AM
How i can "get" coords from sentence:
Pos1 = 12.233, 243.3232, 545423.4321

How i can find ", " ?
String.Sub(string, 7, "index of first ","")
String.Sub(string,"index of first ","" , "index of second ","")
String.Sub(string,"index of second ","" , "index of 3th ","")

Mex
06-02-2009, 09:51 AM
yo zar

How i can find ", " ?

Pos1 = "12.233, 243.3232, 545423.4321"
local FirstCommaPos = string.find(Pos1, ",", 1, true)

param 1 - string to search
param 2 - character(s) to find
param 3 - position to start searching, 1 is the first character in the string, not 0
param 4 - true/false - if false it will perform a regular expression search, if true then it won't

if your wanting a way to find all the numbers seperated by a comma, you could do:

Pos1 = "12.233, 243.3232, 545423.4321"

local SeekPos = 1
local CommaNPos = string.find(Pos1, ",", SeekPos, true)
local TokenN
while CommaNPos do
TokenN = tonumber(string.sub(Pos1, SeekPos, CommaNPos - 1))

SeekPos = CommaNPos + 1
CommandNPos = string.find(Pos1, ",", SeekPos, true)
end
-- last token
TokenN = tonumber(string.sub(Pos1, SeekPos))

i haven't tested that btw

or using VLE's VLEGetToken() function:

Pos1 = "12.233, 243.3232, 545423.4321"
local AmountOfCommas = VLEGetToken(Pos1, 0, ",")
local TokenN
for a=1, AmountOfCommas do
TokenN = tonumber(VLEGetToken(Pos1, a, ","))
end

ZaR
06-02-2009, 10:00 AM
yo mex :)

thank you!

Phobos
06-13-2009, 04:20 PM
Hello.. I need a help ..
I cant rectify whats the problem in this function.
I want this function to make PlayerID speak to all the PlayerID2 if they are near him(15 mts)

function SpeakToPlayer(PlayerID, PlayerID2,Text) --/speak command
local PlayerDistance
local X,Y,Z=getPlayerPos (PlayerID )
outputChatBox(PlayerID,"hey",0,255,0,255)
local i=0
if not text then
outputChatBox(PlayerID,"Use /speak Text")
else
for i=0,25,1 do
local X2,Y2,Z2=getPlayerPos (i)
local PlayerDistance=VLEGetDistance(X,X2, Y, Y2, Z,Z2)
if PlayerDistance<=15 and i~=PlayerID then
PlayerID2=i
PlayerID=tostring(PlayerID)
outputChatBox(PlayerID2,PlayerID..":"..Text,0,255,0,255)
end
end
end
end

addCommand("speak","SpeakToPlayer",2)

Woops Okay I got it to work..
But I didnt checked it if Players near me get the msg to them though.
But script shows me Speaking

Here is the revised code

function SpeakToPlayer(PlayerID,Text)
local PlayerDistance
local PlayerID2
local X,Y,Z=getPlayerPos (PlayerID )
outputChatBox(PlayerID,Text,0,255,0,255)
local i=1
if not text then
outputChatBox(PlayerID,"Use /speak Text")
else
for i=1,25,1 do
PlayerID2=i
local X2,Y2,Z2=getPlayerPos(PlayerID2)
local PlayerDistance=VLEGetDistance(X,X2, Y,Y2, Z,Z2)
if PlayerDistance<=15 and i~=PlayerID then
PlayerID=tostring(PlayerID)
outputChatBox(PlayerID2,PlayerID..":"..Text,0,255,0,255)
end
end
end
end

addCommand("speak","SpeakToPlayer",1)

One more problem.. but I think it is fully correct..
Just I don't know why script crashes..
I couldn't find error in the following program..


function CopRadio(PlayerID,Text)
local PlayerName=getPlayerName(PlayerID)
local PlayerID2
local i=0
local PlayerSkin=getPlayerSkin(PlayerID)
if PlayerSkin==165 then
outputChatBox(PlayerID,"[Law Channel] " .. PlayerName .. ": " .. Text .. ", over!",0,0,255,255)
for i=0,100,1 do
PlayerID2=i
local PlayerSkin2=getPlayerSkin(PlayerID2)
if PlayerSkin2==165 and PlayerID2~=PlayerID then
outputChatBox(PlayerID2,"[Law Channel] Unit " .. PlayerName .. ": " .. Text .. ", over!",0,0,255,255)
end
end
else
gameTextForPlayer(PlayerID,"You are not a Cop",5, 2500)

end
end

addCommand("cr","CopRadio",1)

Boylett
06-14-2009, 05:12 PM
Boylett - I don't think that an extra millisecond or two of processing will really make a difference, considering that it takes at least 10ms for the server to contact a client regardless. ;) Besides, the above method is more conventional.
Heh wait until you get a server with 100 players and your calling it for each player every half a second.

Mex
06-14-2009, 07:55 PM
Phobos try adding a check to see if the player typed any text, something like:

if not Text then
outputChatBox(PlayerID, "You must type some text!", 255, 0, 0, 255)
return
end

Boylett
08-25-2009, 03:51 AM
I wanted something similar to shell_exec in PHP (run a system command and return output), so I found this on the internet which might be useful if you want to get system information:

function os.capture(cmd)
local f = assert(io.popen(cmd, 'r'));
local s = assert(f:read('*a'));
f:close(); s = string.gsub(s, '^%s+', '');
s = string.gsub(s, '%s+$', '');
s = string.gsub(s, '[\n\r]+', ' ');
return s;
end