Shopping Cart
FiveM script How ToVideo Addons how to and converting to an addon

[HowTo] FiveM lua Scripting Full (not but) information – Modding Tutorials

FiveM Modding Tutorial

You’re going to get most worth out of this course if you’ll observe classes it in sequence.

3.1 Creating thread and discovering coordinates
  1. Create new useful resource referred to as my_thread, create fxmanifest.lua file inside your newly created useful resource folder, create shopper.lua file and add it to fxmanifest.

  2. To create thread you should utilize the next code:

Citizen.CreateThread(operate()	
finish)
  1. To be able to create continuously proven marker that you must some time loop.

  2. Whereas loops are loops that repeat code that’s inside it whereas situation is true. On this case we hardcoded it to true, so we can have the actions repeating for so long as participant is in server. You additionally want so as to add Citizen.Wait(0) in any other case sport will crash as soon as loop begins.
    whereas loops are often used when instances of repeats just isn’t recognized prematurely.

Citizen.CreateThread(operate()
	whereas true do
		Citizen.Wait(0)
	finish 
finish)
  1. We’ll create script that continuously prints participant character’s coordinates on his console. All participant controllable characters in addition to non participant characters (NPS’s) in GTA V are described as peds (pedestrians). To be able to get any participant’s coordinates we have to get the ped which is participant controllable entity. Add the next line above your whereas loop.
Citizen.CreateThread(operate()
	native ped = GetPlayerPed(-1)
	whereas true do
		Citizen.Wait(0)
	finish
finish)
  1. No when now we have the participant character id, we will use it to get it’s coordinates. Use GetEntityCoords native operate and insert ped as an argument. Assign it to new variable playerCoords in your whereas loop block after Citizen.Wait(0) operate.

We’re initializing ped variable outdoors of whereas loop as a result of it’s static – participant ped id doesn’t change throughout time. And as you possibly can see we’re initializing playerCoords variable inside whereas loop as it’d change and ought to be continuously refreshed. Not placing ped initialization inside whereas loop permits as to avoid wasting efficiency.

Citizen.CreateThread(operate()
	native ped = GetPlayerPed(-1)
	whereas true do
		Citizen.Wait(0)
		native playerCoords = GetEntityCoords(ped)
		
	finish
finish)
  1. Now that now we have participant coordinates continuously initializing we’re able to print them. Use print and add playerCoords as an argument.
Citizen.CreateThread(operate()
	native ped = GetPlayerPed(-1)
	whereas true do
		Citizen.Wait(0)
		native playerCoords = GetEntityCoords(ped)
		print(playerCoords)
	finish
finish)
  1. The one downside now’s that it is going to be printed each body. To keep away from that put the next wait worth. Lets set it to 5000 which is in milliseconds, so that will translate to five seconds.
Citizen.Wait(5000)
  1. Now you can begin or restart the useful resource and open console utilizing F8. You will note participant coordinates printed each 5 seconds in vector3.
    vector3 is an information sort that shops 3 float numbers. That’s what operate GetEntityCoords returns. To get the numbers individually you possibly can use playerCoords.x, playerCoords.y, playerCoords.z.

Job
Now when you understand how to get participant character’s place strive drawing two markers utilizing DrawMarker native operate. One among them ought to be drawing a marker on some random place in map an one other one immediately on participant.

Hints:

  • Bear in mind that you’ll want to name the operate each body so set Citizen.Wait parameter to 0.

  • Yow will discover what parameters you want in addition to a code instance the hyperlink (it’s best to solely deal with marker’s coordinates for now.) https://runtime.fivem.net/doc/natives/?_0x28477EC23D892089

  • For static marker create new vector3 variable and set coordinates of your alternative to make use of as a parameter for DrawMarker operate.

3.2 Markers that set off motion

You’re going to get most worth out of this course if you’ll observe classes it in sequence.

On this lesson:

  • You’ll write an algorithm that’s fairly widespread for actions that markers set off;

  • Use DrawMarker operate to create markers;

  • Ship chat message to participant when he steps on marker.

For this lesson you’ll need:

  • A information of learn how to create sources;

  • A information of FiveM useful resource construction;

  • Potential to seek out desired coordinates in map.

Introduction

Often markers are supposed to have some performance – when participant steps inside them one thing occurs. Markers don’t try this by them selves, a separate code ought to be added that checks if gamers is in the identical place because the marker and solely then set off the duties.
There’s quite a lot of algorithms and logic that can be utilized with markers. As we speak you can be proven one of the widespread and best ones. This one works fairly properly and simple to jot down when there are a couple of markers to be drawn by the identical useful resource.

  1. Create new useful resource referred to as marker_actions and add shopper.lua file to it.

  2. Add a vector3 that might be used for marker place. And set any coordinates of your alternative

native markerPos = vector3(0.0, 0.0, 0.0)
  1. Within your shopper.lua file create a brand new thread with whereas loop inside that might be used for all marker logic. Additionally register and initialize ped variable which might be used to calculate distance between marker and participant character.
Citizen.CreateThread(operate()
	native ped = GetPlayerPed()
	whereas true do
		Citizen.Wait(0)
	finish
finish)
  1. Now let’s draw the marker utilizing the coordinates. Initially let’s get participant character coordinates and examine how far the participant is from the place of marker. We are able to use lua’s native performance which provides us a capability to get distance between two coordinates written in vectors. Use #(firstVector – secondVector).
Citizen.CreateThread(operate()
	native ped = GetPlayerPed()
	whereas true do
		Citizen.Wait(0)
		native playerCoords = GetEntityCoords(ped)
		native distance = #(playerCoords - markerPos)
	finish
finish)
  1. Now let’s examine how far the participant is from the place of marker and solely show it if participant is shut sufficient (no more than 100 meters away). Additionally if participant is additional away than 100 meters there is no such thing as a must maintain checking if he will get nearer each body, so we will add else block that may wait for two seconds. That signifies that if participant is additional away than 100 meters the gap might be checked each 2 seconds, in any other case each body. That can allow us to to avoid wasting efficiency leaks.
if distance < 100.0 then
	    DrawMarker(1, pedCoords.x, pedCoords.y, pedCoords.z - 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 255, 128, 0, 50, false, true, 2, nil, nil, false)
	else
	    Citizen.Wait(2000)
finish
  1. Hook up with server, begin the useful resource and it is possible for you to to see the marker in coordinates you assigned for it until you get additional away than 100 meters.
  2. Proper above if assertion add new variable referred to as isInMarker and assign false to it.
native isInMarker = false
  1. But it surely nonetheless doesn’t have any performance and nothing occurs for those who step inside it. That we’ll implement now. Inside if block add one other if assertion that may examine if participant is nearer than 2 meters away from marker which might be contained in the marker. In that case assign true to isInMarker variable and false in any other case.
if distance < 100.0 then
	    DrawMarker(1, pedCoords.x, pedCoords.y, pedCoords.z - 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 255, 128, 0, 50, false, true, 2, nil, nil, false)
	if distance < 2.0 then
		isInMarker = true
	finish
else
	Citizen.Wait(2000)
finish
  1. Now on the very backside of thread operate we will implement any performance for the marker. Let’s simply print some message to participant’s chat. Add the next on the backside of thread operate.
if isInMarker then
 	TriggerEvent('chatMessage', 'You might be contained in the marker')
finish
  1. Should you would be part of the server, begin the useful resource you’d see that you just maintain getting the message continuously, that occurs as a result of the chat message is triggered each body, along with DrawMarker operate
  2. To keep away from it we have to examine if shopper has already acquired the message. On the prime of you file initialize new variable referred to as HasAlreadyGotMessage and assign false to it.
HasAlreadyGotMessage = false
  1. Then add an else block to assertion which checks if gamers is lower than 2 meters away. That can reset HasAlreadyGotMessage to false, so after participant strikes away from marker after which steps again in he’ll get message once more.
if distance < 2.0 then
	isInMarker = true
else
	HasAlreadyGotMessage = false
finish
  1. And alter if block which sends the message. This may examine if participant has already acquired the message and set HasAlreadyGotMessage to true, as a result of the message is not going to be despatched
if isInMarker and never HasAlreadyGotMessage then
	TriggerEvent('chatMessage', 'You might be contained in the marker')
	HasAlreadyGotMessage = true
finish



Authentic supply: https://discussion board.cfx.re/t/howto-fivem-lua-scripting-complete-not-yet-guide/1467797

Leave a Reply
FiveM ESX Scripts

The best scripts and maps

Best support

Problems? We are there to help!

Wide range of products

We have a wide range of products

100% Secure Checkout

Stripe