Debugging Resources
Techniques and tools for debugging FiveM resources.
Console Output​
Use print() for basic debugging:
print('Debug message')
print('Player health: ' .. GetEntityHealth(PlayerPedId()))
View console output:
- Client: Press
~in-game (backtick key) - Server: Look at the server console window
Debug Convars​
Enable debug logging:
-- In server.cfg
set debug_mode true
-- Access in Lua
if GetConvar('debug_mode', 'false') == 'true' then
print('Debug mode enabled!')
end
Common Debugging Methods​
Checking Conditions​
local playerPed = PlayerPedId()
if playerPed == 0 then
print("Error: Invalid player ped!")
return
end
Logging Table Data​
local function logTable(t, indent)
indent = indent or 0
for key, value in pairs(t) do
local formatting = string.rep(" ", indent) .. key .. ": "
if type(value) == 'table' then
print(formatting)
logTable(value, indent + 1)
else
print(formatting .. tostring(value))
end
end
end
logTable(Config)
Event Debugging​
-- Debug all events
AddEventHandler('__cfx_internal:serverErrorEvent', function(data)
print('Server Error:', data)
end)
Performance Profiling​
Check resource performance:
# In console
profiler record my-resource 10000 -- Profile for 10 seconds
profiler dump
Error Handling​
Wrap code in error handlers:
local function safeCall(func, ...)
local success, result = pcall(func, ...)
if not success then
print('Error: ' .. result)
return nil
end
return result
end
safeCall(function()
-- Your code here
end)
Testing Resource​
Always test your resource:
- Use a local development server
- Test with multiple players if possible
- Check console for errors
- Monitor performance
- Test edge cases and error conditions
VSCode Debugging​
Install the FiveM extension for VSCode to get better debugging support and syntax highlighting.