Final project: your first ATM · Lesson 2/4 · 10 min

The skeleton and the ATM zone

fxmanifest, config with the coords and a client that detects when you're nearby (with dynamic Wait).

lua
fx_version 'cerulean'
game 'gta5'
author 'TuNombre'
description 'Cajero simple'
version '1.0.0'

shared_scripts {
  '@es_extended/imports.lua',
  'config.lua',
}
client_scripts { 'client.lua' }
server_scripts { 'server.lua' }

fxmanifest.lua

lua
Config = {}
Config.Tecla = 38            -- E
Config.Distancia = 1.5
Config.Cajeros = {
  vector3(147.4, -1035.5, 29.3),
  vector3(-1212.9, -331.9, 37.7),
}

config.lua

lua
-- client.lua: detect proximity with dynamic Wait (performance module)
CreateThread(function()
  while true do
    local sleep = 1000
    local ped = PlayerPedId()
    local coords = GetEntityCoords(ped)
    for _, cajero in ipairs(Config.Cajeros) do
      if #(coords - cajero) < Config.Distancia then
        sleep = 0
        -- simple help text
        BeginTextCommandDisplayHelp('STRING')
        AddTextComponentSubstringPlayerName('Press ~INPUT_PICKUP~ to use the ATM')
        EndTextCommandDisplayHelp(0, false, true, -1)
        if IsControlJustReleased(0, Config.Tecla) then
          abrirCajero()
        end
      end
    end
    Wait(sleep)
  end
end)

client.lua

Notice the dynamic Wait: it rests 1s and only runs per frame when you're near an ATM. That way the resource is practically free on performance.

Practice what you learned

0/3
Test

¿Qué calcula #(coords - cajero) dentro del bucle del cliente?

Pista

El símbolo # sobre la resta de dos vector3 es la longitud (distancia).

Rellena los huecos

Completa el Wait dinámico: 0 mientras estás cerca, y descansa el resto del tiempo.

1if #(coords - cajero) < Config.Distancia then
2 sleep =
3end
4Wait()
Pista

Cerca: sleep = 0 para ir por frame. Siempre se espera la variable: Wait(sleep).

Corrige el error

Este bucle corre cada frame siempre y mata el rendimiento. Arréglalo con un Wait dinámico (descansa cuando no estás cerca).

Este código tiene un fallo:

1CreateThread(function()
2 while true do
3 local coords = GetEntityCoords(PlayerPedId())
4 for _, cajero in ipairs(Config.Cajeros) do
5 if #(coords - cajero) < Config.Distancia then
6 -- mostrar 'Pulsa E'
7 end
8 end
9 Wait(0)
10 end
11end)

Reescríbelo corregido:

Pista

Crea local sleep = 1000 al inicio del bucle, ponlo a 0 cuando estés cerca y usa Wait(sleep).

Challenge: code it yourself

Add a third ATM at other coordinates and test that the text appears at all three.

Write it yourself in your editor (VS Code) and test it on your server. You learn here by doing it, not by copying.

See hint

Add another vector3 to Config.Cajeros; get coords with a command that prints GetEntityCoords.

Escribe aquí tu solución:

How was this lesson?