GTAOnline.net

Multi Theft Auto (mta) => Scripting MTA [Lua Center] => Discussion démarrée par: Ssk le 25 Mai 2010, 19:11:07

Titre: Code / scripts utiles.
Posté par: Ssk le 25 Mai 2010, 19:11:07
Salut tout le monde !

Voila un post ou vous pouvez poster / trouver des codes utiles pour vos script.


Voila je lance le sujet avec une fonction qui permet de connaitre la distance entre deux points.


function GetDistanceBetween(x,y,z,x1,y1,z1)
return math.sqrt( math.pow((x - x1),2) + math.pow((y - y1),2) + math.pow((z - z1),2) )
end


Et une commande pour spawn des véhicules en écrivant le nom complet et vous pouvez mettre les couleur en options

exemple

/v infernus 6 6
permet de spawn une infernus jaune =)

function CreatePlayerVehicle(playerid,command,name,color,color1)
local x,y,z = getElementPosition(playerid)
if(getPedOccupiedVehicle (playerid))then
u,i,angle = getVehicleRotation(getPedOccupiedVehicle (playerid))
else
angle = getPedRotation(playerid)
end
local modelid = getVehicleModelFromName(name)
if(modelid)then
local vehid = createVehicle(modelid,x + (5 * math.sin(math.rad(-angle))),y + (5 * math.cos(math.rad(-angle))),z + 2,0,0,angle + 90,"Sasuke78200")
if(color and color1)then
setVehicleColor(vehid,tonumber(color),tonumber(color1),0,0)
end
end
end
addCommandHandler("v",CreatePlayerVehicle)
Titre: Re : Code / scripts utilies.
Posté par: Redouane le 13 Septembre 2010, 18:11:47
Salut tu pourrait détailler comment mettre car je suis débutant x)  ???


Merci
Titre: Re : Code / scripts utilies.
Posté par: Citizen le 17 Décembre 2010, 21:46:28
Voici une commande qui permet de se téléporter aux coordonnées x,y,z:
Code: (lua) [Sélectionner]
function goToThisPos( thePlayer, commandName, x, y, z )
if ( x and y and z ) then
setElementPosition( thePlayer, x, y, z )
outputChatBox("Vous avez été téléporté à la position "..x..", "..y..", "..z, thePlayer, 0, 180, 0)
else
outputChatBox("Vous devez préciser 3 valeurs ( x, y, z ) !", thePlayer, 180, 0, 0 )
end
end
addCommandHandler( "goto", goToThisPos, false, false )

Une commande pour se suicider:
Code: (lua) [Sélectionner]
function seSuicider( thePlayer, commandName )
setElementHealth( thePlayer, 0 )
outputChatBox("Vous vous êtes suicidé !", thePlayer, 180, 0, 0 )
end
addCommandHandler( "kill", seSuicider, false, false )

Une commande pour se donner de la vie ( ou en enlever  :) ):
Code: (lua) [Sélectionner]
function setSante( thePlayer, commandName, sante )
if ( sante )
setElementHealth( thePlayer, sante)
outputChatBox("Vous avez désormais "..sante.." vies.", thePlayer, 0, 180, 0)
else
outputChatBox("SYNTHAXE: /sante [nbDeVies] ", thePlayer, 180, 180, 180)
end
end
addCommandHandler( "sante", setSante, false, false )


Une commande qui vous permet de changer d'intérieur:
Code: (lua) [Sélectionner]
function int( thePlayer, commandName, intID )
if( intID )then
setElementInterior( thePlayer, intID )
outputChatBox( "Vous vous êtes assigné l'intérieur "..intID..".", thePlayer, 0, 180, 0)
else
outputChatBox( "SYNTHAXE: /interieur [InterieurID] ", thePlayer, 175, 175, 175 )
end
end
addCommandHandler( "setint", int, false, false )

Une commande pour sauvegarder la position actuelle de votre perso, sa rotation et son intérieur et vous permet de rajouter un commentaire dans un fichier appelé savedpositions.txt :
Code: (lua) [Sélectionner]
function Save( thePlayer, commandName, ... )
local commentaire1 = {...}
local commentaire = table.concat( commentaire1, " " )
if( isAdmin( thePlayer, 1 ) )then
local x,y,z = getElementPosition( thePlayer )
local rx = getPedRotation( thePlayer )
local int = getElementInterior( thePlayer )
local savefile
savefile = fileOpen( "savedpositions.txt" )
if ( not savefile )then
fileCreate("savedpositions.txt")
savefile = fileOpen( "savedpositions.txt" )
end
if ( savefile ) then
fileSetPos( savefile, fileGetSize( savefile ) )
if ( commentaire ) then
local writen = fileWrite( savefile, "\n"..x..", "..y..", "..z..", "..rx..", "..int.." --"..commentaire.."\r\n" )
if( writen ) then
outputChatBox( "Position sauvegardée", thePlayer, 0, 200, 0 )
else
outputChatBox( "Impossible de sauvegarder la position", thePlayer, 200, 0, 0 )
end
else
local writen = fileWrite( savefile, "\n"..x..", "..y..", "..z..", "..rx..", "..int.."\r\n" )
if( writen ) then
outputChatBox( "Position sauvegardée", thePlayer, 0, 200, 0 )
else
outputChatBox( "Impossible de sauvegarder la position", thePlayer, 200, 0, 0 )
end
end
end
fileClose( savefile )
else
outputChatBox( "Vous n'êtes pas Modérateur/Administrateur", thePlayer, 200, 0, 0 )
end
end
addCommandHandler("save", Save, false, false )

EXEMPLE:              /save Spawn Taxi

Je vais avoir quelque chose de ce genre la dans le fichier savedpositions.txt :

1753.2275390625, -1894.0634765625, 13.557225227356, 271.66583251953, 0 --Taxi Spawn

Voilà j'en rajouterai surement d'ici 2 semaines ( très occupé pendant ces vacs  :P )

Si vous avez des questions, n'hésitez pas.
Titre: Re : Code / scripts utilies.
Posté par: Citizen le 30 Décembre 2010, 19:44:13
Bonjour !
Me revoici avec de nouvelles commandes scripté pendant mes heures perdues  :P

Une cmd pour faire du saut en parachute au-dessus de votre position actuelle:
Code: (lua) [Sélectionner]
function skyDive( thePlayer, commandName )
local x, y, z = getElementPosition( thePlayer )
local veh = getPedOccupiedVehicule( thePlayer )
if ( not veh ) then
giveWeapon( thePlayer, 46, 1 )
setElementPosition( thePlayer, x, y, z+1000 )
outputChatBox( " GO GO GO !! ", thePlayer )
else
outputChatBox( " Vous devez d'abord sortir de votre véhicule ! ", thePlayer )
end
end
addCommandHandler( "basejump", skyDive, false, false )

Une cmd pour afficher dans votre chatbox le nombre actuelle de joueurs:
Code: (lua) [Sélectionner]
function howManyPlayers( thePlayer, commandName )
local nbPlayers = 0
for k, i in ipairs ( getElementsByType( "player" ) )do
nbPlayers = nbPlayers+1
end
outputChatBox( " Il y a actuellement "..nbPlayers.." joueurs sur le server ", thePlayer )
end
addCommandHandler( "joueurs", howManyPlayers, false, false )

Une cmd pour freezer un joueur:
Code: (lua) [Sélectionner]
function freezeAPlayer( thePlayer, commandName, targPlayerName )
if ( targPlayerName ) then
local targPlayer = getElementByName( ""..targPlayerName )
if ( targPlayer ) then
local frozen = getPedFrozen( targPlayer )
setPedFrozen( targPlayer, not frozen )
if ( not frozen == true ) then
outputChatBox( "Vous avez freezer "..targPlayerName.." .", 180, 0, 0 )
outputChatBox( "Vous avez été freezé par "..getPlayerName( thePlayer ).." .", 0, 180, 0 )
else
outputChatBox( "Vous avez défreezer "..targPlayerName.." .", 180, 0, 0 )
outputChatBox( "Vous avez été défreezé par "..getPlayerName( thePlayer ).." .", 0, 180, 0 )
end
else
outputChatBox( "* Aucun joueur ne porte ce nom !", thePlayer, 180, 0, 0 )
end
else
outputChatBox( "SYNTAXE: /freeze [NomDuJoueur]", thePlayer )
end
end
addCommandHandler( "freeze", freezeAPlayer, false, false )

Une cmd pour changer la couleur de votre véhicule:
Code: (lua) [Sélectionner]
function changeColor( thePlayer, commandName, color1, color2, color3, color4 )
local color1 = color1 or 0
local color2 = color2 or 0
local color3 = color3 or 0
local color4 = color4 or 0
local veh = getPedOccupiedVehicle( thePlayer )
if ( veh ) then
setVehicleColor( veh, color1, color2, color3, color4)
outputChatBox( "Vous avez changé la couleur du véhicule !", thePlayer, 0, 180, 0 )
else
outputChatBox( "Vous devez être dans un véhicule !", thePlayer, 180, 0, 0 )
end
end
addCommandHandler( "vehcolor", changeColor, false, false )


Une cmd pour changer le skin d'un joueur:
Code: (lua) [Sélectionner]
function setSkin( thePlayer, commandName, targPlayerName, skin )
if ( targPlayerName and skin ) then
local targPlayer = getElementByName( ""..targPlayerName )
if ( targPlayer ) then
setElementModel( targPlayer, skin )
outputChatBox( "Vous avez mis le skin "..skin.." à "..targPlayerName.." .", 180, 0, 0 )
outputChatBox( getPlayerName( thePlayer ).." vous a mis le skin "..skin.." .", 0, 180, 0 )
else
outputChatBox( "* Aucun joueur ne porte ce nom !", thePlayer, 180, 0, 0 )
end
else
outputChatBox( "SYNTAXE: /skin [NomDuJoueur] [skinID]", thePlayer )
end
end
addCommandHandler( "skin", setSkin, false, false )

Une cmd pour changer la météo du server:
Code: (lua) [Sélectionner]
function setServerWeather( thePlayer, commandName, weather )
if ( weather ) then
setWeather( weather )
outputChatBox( "La météo à été changé par "..getPlayerName( thePlayer ) )
else
outputChatBox( "SYNTAXE: /meteo [meteoID 0-255]", thePlayer )
end
end
addCommandHandler( "meteo", setServerWeather, false, false )

Et enfin une cmd pour changer l'heure du server:
Code: (lua) [Sélectionner]
function setServerTime( thePlayer, commandName, heure, minute )
if ( heure ) then
if ( minute )then
setTime( heure, minute )
else
setTime( heure, 0 )
end
local notifyMessage = string.format("Le temps est maintenant %02d:%02d !", hour, minute)
outputChatBox ( notifyMessage )
else
outputChatBox( "SYNTAXE: /temps [heure 0-23] ( [minute 0-59] )", thePlayer )
end
end
addCommandHandler( "temps", setServerTime, false, false )

Voilà voilà

Si vous avez des suggestions de cmd n'hésitez pas  :)

Cordialement,

                         Citizen

EDIT: Correction dans le 1er code
Titre: Re : Code / scripts utilies.
Posté par: Ssk le 30 Décembre 2010, 19:57:50
J'ai pas lu tout les codes mais y a une erreur dans le premier
setElementPosition( x, y, z+1000 )serai mieux en
setElementPosition( thePlayer, x, y, z+1000 )
Titre: Re : Code / scripts utilies.
Posté par: MK le 30 Décembre 2010, 20:37:11
Merci  de partager tous ca, je regarde ca, voir comment vous faites etc.
Titre: Re : Code / scripts utilies.
Posté par: Citizen le 30 Décembre 2010, 20:48:42
 :lmao
oui merci ( tu aurais pu me le dire discretos par msn  :D )
La faute à été corrigée

                 Citizen
Titre: Re : Re : Code / scripts utilies.
Posté par: Ssk le 31 Décembre 2010, 12:56:50
:lmao
oui merci ( tu aurais pu me le dire discretos par msn  :D )
La faute à été corrigée

                 Citizen
Aha je note ^^
Titre: Re : Code / scripts utiles.
Posté par: Citizen le 11 Janvier 2011, 22:18:58
A oui enfait j'avais pas fait attention mais dans ton premier post Sasu pourquoi n'utilise pas tout simplement la fonction
Code: (lua) [Sélectionner]
getDistanceBetweenPoints3D ( float x1, float y1, float z1, float x2, float y2, float z2 ) :lmao

Se serai plus pratique tu ne trouves pas ? mais sinon +1 pour l'avoir refait  :D

Cordialement,
                       Citizen
Titre: Re : Code / scripts utiles.
Posté par: ~CG~FatalTerror le 18 Avril 2011, 19:24:48
Commande pour démarrer une musique:

function playMusic()
playSound ("music.mp3", false)
-- Note: The only supported audio formats are MP3, WAV, OGG, RIFF, MOD, XM, IT and S3M.
end
addCommandHandler ( "music", playMusic )


Heu je crois que c'est correct  :-\
Titre: Re : Code / scripts utiles.
Posté par: Ssk le 18 Avril 2011, 20:20:46
on a pas dis de prendre des codes directement pris du wikipedia ^^

De plus il faut aussi dire que ce code ne fonctionne qu'en client.
Titre: Re : Code / scripts utiles.
Posté par: Citizen le 18 Avril 2011, 23:00:58
Mis à part que ça provient du Wiki tout court et non pas du Wikipedia  :P
 :jesors
Titre: Re : Code / scripts utiles.
Posté par: ~CG~FatalTerror le 19 Avril 2011, 00:37:37
Ce code est sur le wiki  ???
Désolé  :bangin

Commande pour faire une sorte de loto ( Je l'ai fait pour mon RP  :-X )
J'ai bien commenté !  ::)
(Coté server)

function miserLoto()
local argent = getPlayerMoney(source)
if (argent > 1000) then -- On vérifie si le joueur a plus de 1000$
takePlayerMoney(source, 1000) -- On prend 1000$ du joueur
local hasard = math.random(1,10) -- Nombre au hasard entre 1 et 10
local hasard2 = math.random(1000,5000) -- Nombre au hasard entre 1000 et 5000
if (hasard == 5) then -- On vérifie si le chiffre est egal a 5
local argentaGagner = hasard*hasard2 -- On multiplie le nombre hasard par l'argent que le joueur va gagner
givePlayerMoney(source,argentaGagner) -- On donne l'argnet
local nomDuGagnant = getPlayerName(source) -- On prend le nom du joueur
outputChatBox("#ff0000[Loto]#00aaff"..nomDuGagnant.." a gagné "..argentaGagner.."$ au loto ! (Petit venar >.<)", getRootElement(), 255, 0, 0, true)
else -- Si le chiffre n'es pas 5 on lance un message
outputChatBox("#ff0000[Loto]#00aaffVous avez #ff0000PERDU !!!", source, 255, 0, 0, true)
end
else -- Si le joueur n'a pas 1000$ on lanceun message
outputChatBox("#ff0000[Loto]#00aaffVous n'avez pas 1000$ pour jouer !", source, 255, 0, 0, true)
end
  end
 addCommandHandler ( "loto", miserLoto )


Vous me dites si j'ai fait des erreurs (chui qu'un débutant  :dry )
Titre: Re : Code / scripts utiles.
Posté par: Citizen le 19 Avril 2011, 18:48:17
Nan c'est tout bon merci du partage  :happy
Titre: Re : Code / scripts utiles.
Posté par: ~CG~FatalTerror le 19 Avril 2011, 22:42:23
Ouais ! Pour une fois que j'ai pas fait d'erreur  :lmfao
Titre: Re : Code / scripts utiles.
Posté par: Ssk le 20 Avril 2011, 02:07:20
Y a de l'optimisation à faire ^^
Titre: Re : Code / scripts utiles.
Posté par: ~CG~FatalTerror le 20 Avril 2011, 02:42:12
Désolé je peux pas faire plus précis  :lmao

Commande pour sauter(Serverside)  :rules

function push( thePlayer )

local voiture = getPedOccupiedVehicle( thePlayer )
local x, y, z = getElementVelocity ( voiture )
setElementVelocity ( voiture, x , y, z + 0.4 )
end
addCommandHandler("push", push)

A tester  :-\
Titre: Re : Code / scripts utiles.
Posté par: CafeOLait le 23 Janvier 2012, 20:31:04
Moi je dépose un petit script pour l'ouverture de barrières avec une commande

gatea = createObject ( 969, 1397.1, 2701.6, 9.8, 0, 0, 270 )
gateb = createObject ( 969, 1397.1, 2693.6, 9.8, 0, 0, 270 )
gatec = createObject ( 987, 2294.6, 2503.2, 2.3, 0, 0, 274 )
gated = createObject ( 987, 2319.8, 2440.5, 2.3, 0, 0, 90 )
gateea = createObject ( 987, 2245.6, 2481.5, 14, 0, 0, 270 )
gateeb = createObject ( 987, 2245.6, 2469.5, 14, 0, 0, 270 )
gateec = createObject ( 987, 2245.6, 2457.5, 14, 0, 0, 270 )
gateed = createObject ( 987, 2245.6, 2445.5, 14, 0, 0, 270 )
arene = createObject ( 17018, -1417, 363, 25, 0, 0, 270 )
airport = createObject ( 987, -1549.8, -426, 5, 0, 0, 316 )
arene2 = createObject ( 3277, -1426.8, 368.4, 26.1, 0, 0, 0 )

-- create the function the command handler calls, with the arguments: thePlayer, command, vehicleModel
function ouvrirdepot(commandName, posX, posY, posZ)
   moveObject ( gatea, 15000, 1397.1, 2689, 9.8 )
   moveObject ( gateb, 15000, 1397.1, 2689, 9.8 )
end

addCommandHandler("od", ouvrirdepot)

-- create the function the command handler calls, with the arguments: thePlayer, command, vehicleModel
function fermerdepot(commandName, posX, posY, posZ)
   moveObject ( gatea, 15000, 1397.1, 2701.6, 9.8 )
   moveObject ( gateb, 15000, 1397.1, 2693.6, 9.8 )
end

addCommandHandler("fd", fermerdepot)

function ouvrirairport(commandName, posX, posY, posZ)
   moveObject ( airport, 15000, -1557.5, -418, 5 )
end

addCommandHandler("oa", ouvrirairport)

-- create the function the command handler calls, with the arguments: thePlayer, command, vehicleModel
function fermerairport(commandName, posX, posY, posZ)
   moveObject ( airport, 15000, -1549.8, -426, 5 )
end

addCommandHandler("fa", fermerairport)

function op(commandName, posX, posY, posZ)
   moveObject ( gatec, 10000, 2294.6, 2503.2, 7 )
   moveObject ( gated, 10000, 2319.8, 2440.5, 8 )
end

addCommandHandler("op", op)

-- create the function the command handler calls, with the arguments: thePlayer, command, vehicleModel
function fp(commandName, posX, posY, posZ)
   moveObject ( gatec, 10000, 2294.6, 2503.2, 2.3 )
   moveObject ( gated, 10000, 2319.8, 2440.5, 2.3 )
end

addCommandHandler("fp", fp)

function secure(commandName, posX, posY, posZ)
   moveObject ( gateea, 10000, 2245.6, 2481.5, 9.8 )
   moveObject ( gateeb, 10000, 2245.6, 2469.5, 9.8 )
   moveObject ( gateec, 10000, 2245.6, 2457.5, 9.8 )
   moveObject ( gateed, 10000, 2245.6, 2445.5, 9.8 )
end

addCommandHandler("secure", secure)

function unsecure(commandName, posX, posY, posZ)
   moveObject ( gateea, 10000, 2245.6, 2481.5, 14 )
   moveObject ( gateeb, 10000, 2245.6, 2469.5, 14 )
   moveObject ( gateec, 10000, 2245.6, 2457.5, 14 )
   moveObject ( gateed, 10000, 2245.6, 2445.5, 14 )
end

addCommandHandler("unsecure", unsecure)

function up(commandName, posX, posY, posZ)
   moveObject ( arene, 30000, -1417, 363, 32 )
   moveObject ( arene2, 30000, -1426.8, 368.4, 36.8 )
end

addCommandHandler("up", up)

function down(commandName, posX, posY, posZ)
   moveObject ( arene, 30000, -1417, 363, 25 )
   moveObject ( arene2, 30000, -1426.8, 368.4, 26.1 )
end

addCommandHandler("down", down)


Bonne soirée
Titre: Re : Code / scripts utiles.
Posté par: vincentdu90 le 23 Mai 2012, 10:58:14
Ce n'est pas un script/code mais je me permets de mettre ce lien ici : http://www.luteus.biz/Download/LoriotPro_Doc/LUA/LUA_Training_FR/Introduction_Programmation.html (http://www.luteus.biz/Download/LoriotPro_Doc/LUA/LUA_Training_FR/Introduction_Programmation.html)

Un super tutoriel pour apprendre la programmation en Lua pure, pas celui relatif à Mta:Sa mais c'est à peu près la même chose.

(Je sais pas si c'est considéré comme de la Pub étant donné que ça n'a pas vraiment de rapport à Gta Sa)