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).
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
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
-- 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¿Qué calcula #(coords - cajero) dentro del bucle del cliente?
Pista
El símbolo # sobre la resta de dos vector3 es la longitud (distancia).
Completa el Wait dinámico: 0 mientras estás cerca, y descansa el resto del tiempo.
if #(coords - cajero) < Config.Distancia then sleep = endWait()Pista
Cerca: sleep = 0 para ir por frame. Siempre se espera la variable: Wait(sleep).
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:
CreateThread(function() while true do local coords = GetEntityCoords(PlayerPedId()) for _, cajero in ipairs(Config.Cajeros) do if #(coords - cajero) < Config.Distancia then -- mostrar 'Pulsa E' end end Wait(0) endend)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:
