[OLD Archive] The Basics Getting Started Scripting Examples - JayMontana36/LuaPlugin-GTAV GitHub Wiki
Function - loop
The loop()
function is what's run on every game frame; be careful of what you put here as you can potentially freeze or crash your game, but such is life - trial and error, live and learn. You can actually be(come) quite creative here, skipping a few frames if you don't want or need your code to be run on every frame, by utilizing a counting/number variable and an if statement for example.
Example #1 (Very Basic And Barebones):
--[ (Optionally) Localize the print function for faster access ](/JayMontana36/LuaPlugin-GTAV/wiki/-(Optionally)-Localize-the-print-function-for-faster-access-)
local print = print
return {
loop = function()
print("Hello World!")
end
}
Example #2 (Basic/Wait):
--[ (Optionally) Localize the print function for faster access aka lower latency and etc. ](/JayMontana36/LuaPlugin-GTAV/wiki/-(Optionally)-Localize-the-print-function-for-faster-access-aka-lower-latency-and-etc.-)
local print = print
--[ Create an integer number variable to store the time in for when your script should run next ](/JayMontana36/LuaPlugin-GTAV/wiki/-Create-an-integer-number-variable-to-store-the-time-in-for-when-your-script-should-run-next-)
local TimeToWaitUntil = 0 -- Default to 0 on script startup
return {
--[ Info - a table variable containing various common information from within the game ](/JayMontana36/LuaPlugin-GTAV/wiki/-Info---a-table-variable-containing-various-common-information-from-within-the-game-)
loop = function(Info)
--[ If it is time for the script to run again then ](/JayMontana36/LuaPlugin-GTAV/wiki/-If-it-is-time-for-the-script-to-run-again-then-)
if Info.Time >= TimeToWaitUntil then
--[ Do Stuff ](/JayMontana36/LuaPlugin-GTAV/wiki/-Do-Stuff-)
print("Hello World! 5 seconds or 5000 milliseconds have passed!")
--[ Update the integer number variable with the current time and the amount of time to wait for next run ](/JayMontana36/LuaPlugin-GTAV/wiki/-Update-the-integer-number-variable-with-the-current-time-and-the-amount-of-time-to-wait-for-next-run-)
TimeToWaitUntil = Info.Time + 5000
end
end
}
The "Info" variable (1st argument sent to loop functions) and game native function call examples:
Structure/Layout:
{
Time --[ integer number ](/JayMontana36/LuaPlugin-GTAV/wiki/-integer-number-)
Player {
Id --[ Id/Handle ](/JayMontana36/LuaPlugin-GTAV/wiki/-Id/Handle-)
Ped --[ Ped/Entity Id/Handle ](/JayMontana36/LuaPlugin-GTAV/wiki/-Ped/Entity-Id/Handle-)
Coords --[ Vector3 ](/JayMontana36/LuaPlugin-GTAV/wiki/-Vector3-)
Vehicle {
IsIn --[ boolean - Is Player In The Vehicle ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean---Is-Player-In-The-Vehicle-)
IsOp --[ boolean - Is Player Operating The Vehicle ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean---Is-Player-Operating-The-Vehicle-)
Id --[ Vehicle/Entity Id/Handle ](/JayMontana36/LuaPlugin-GTAV/wiki/-Vehicle/Entity-Id/Handle-)
NetId --[ integer Network Id/Handle ](/JayMontana36/LuaPlugin-GTAV/wiki/-integer-Network-Id/Handle-)
Model --[ hash ](/JayMontana36/LuaPlugin-GTAV/wiki/-hash-)
Name --[ string ](/JayMontana36/LuaPlugin-GTAV/wiki/-string-)
Type {
Bicycle --[ boolean ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean-)
Bike --[ boolean ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean-)
Boat --[ boolean ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean-)
Car --[ boolean ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean-)
Heli --[ boolean ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean-)
Jetski --[ void - nothing ](/JayMontana36/LuaPlugin-GTAV/wiki/-void---nothing-)
Plane --[ boolean ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean-)
Quadbike --[ boolean ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean-)
Train --[ boolean ](/JayMontana36/LuaPlugin-GTAV/wiki/-boolean-)
AmphibiousCar --[ void - nothing ](/JayMontana36/LuaPlugin-GTAV/wiki/-void---nothing-)
AmphibiousQuadbike --[ void - nothing ](/JayMontana36/LuaPlugin-GTAV/wiki/-void---nothing-)
}
}
}
}
Usage Examples:
return {
loop = function(Info)
--[ Get the player's ped entity ID ](/JayMontana36/LuaPlugin-GTAV/wiki/-Get-the-player's-ped-entity-ID-)
local PlayerPed = Info.Player.Ped
--[ FiveM style game native function call ](/JayMontana36/LuaPlugin-GTAV/wiki/-FiveM-style-game-native-function-call-)
SetEntityInvincible(PlayerPed, false)
--[ Older/Legacy style game native function call ](/JayMontana36/LuaPlugin-GTAV/wiki/-Older/Legacy-style-game-native-function-call-)
ENTITY.SET_ENTITY_INVINCIBLE(PlayerPed, false)
--[ Check if the player is in a vehicle and if the player is the operator of a vehicle ](/JayMontana36/LuaPlugin-GTAV/wiki/-Check-if-the-player-is-in-a-vehicle-and-if-the-player-is-the-operator-of-a-vehicle-)
if Info.Player.Vehicle.IsIn and Info.Player.Vehicle.IsOp then
--[ Get the player's vehicle entity ID ](/JayMontana36/LuaPlugin-GTAV/wiki/-Get-the-player's-vehicle-entity-ID-)
local PlayerVeh = Info.Player.Vehicle.Id
--[ FiveM style game native function call ](/JayMontana36/LuaPlugin-GTAV/wiki/-FiveM-style-game-native-function-call-)
SetVehicleNumberPlateText(PlayerVeh, "0 HI 0")
--[ Older/Legacy style game native function call ](/JayMontana36/LuaPlugin-GTAV/wiki/-Older/Legacy-style-game-native-function-call-)
VEHICLE.SET_VEHICLE_NUMBER_PLATE_TEXT(PlayerVeh, "0 HI 0")
end
end