Skip to content

Instantly share code, notes, and snippets.

@DemmyDemon
DemmyDemon / format_table.lua
Created May 23, 2024 11:53
I needed to putput some tabulated data, and realized I trivially could make it output Markdown.
--- Transforms a table of tables into a possibly Markdown-formatted table
---@param tbl table The table to be sorted
---@param sorted boolean? If true, the resulting table will be very naïvely sorted.
---@param skipDivider boolean? If true, a divider line is omitted. This line is required for Markdown!
---@return table lines Table of strings containing the lines of the table. Just iterate and print.
function FormatTable(tbl, sorted, skipDivider)
local width = {} -- Stores the width of each column.
local maxIndex = 0 -- Stores the highest number of columns seen for a row
local lines = {} -- Will store the actual output
@DemmyDemon
DemmyDemon / server_call.lua
Created February 12, 2024 20:34
Server Calls shared_script for FiveM
if IsDuplicityVersion() then
local calls = {}
---Define a server-side call that a client can use to run code on the server.
---@param name string The uniquely identifying name of this call.
---@param code function The function to run when this call is made
---@return table eventData The event data associated with the event that was registered to handle this call.
function DefineServerCall(name, code)
local finalName = string.format("%s:RPC:%s", GetCurrentResourceName(), name)
if calls[finalName] then
@DemmyDemon
DemmyDemon / where-is-everyone.lua
Created December 15, 2023 19:50
HTTP request handler to get player locations
local function handler(request, response)
local key = GetConvar("playerLocationKey", "")
if key ~= "" then
if request.headers.Authorization == nil or request.headers.Authorization ~= "Bearer " .. key then
response.writeHead(403, {["Content-Type"] = "text/plain"})
response.send("Invalid Bearer Token")
return
end
end
@DemmyDemon
DemmyDemon / server.lua
Created November 19, 2023 13:39
Deferral and database lookup of player (entirely untested, educational purposes only)
AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals)
-- First, a local copy of source, so it doesn't get lost while deferring.
local source = source
-- Actually defer the connection
deferrals.defer()
-- Wait a tick, so the deferral happens.
Citizen.Wait(0)
-- Tell the client what we're doing, in case it stalls at this point, somehow.
@DemmyDemon
DemmyDemon / cl_view.lua
Created October 5, 2023 06:55
Orbital camera
-- This code is ANCIENT, and probably contains natives that have been renamed.
-- It also contains references to functions not provided here.
-- Either way, it should illustrate how to orbit a camera around a target Ped.
function moveCamera()
if IsEntityAPed(targetPed) then
local targetCoords = nil
if IsPedInAnyVehicle(targetPed,false) then
local vehicle = GetVehiclePedIsIn(targetPed,false)
@DemmyDemon
DemmyDemon / partial___sv_area_editor.lua
Created August 7, 2023 10:36
Discussing loading of data files in FiveM
DefineNightCallback('editor-save-json', function(source, jsonString)
if IsPlayerAceAllowed(source, 'admin') then
local data = json.decode(jsonString)
if data and data.name then
local name = string.lower(data.name)
name = name:gsub("[^a-z.]", "_")
name = name .. '.json'
local saved = SaveResourceFile(GetCurrentResourceName(), 'areas/'..name, jsonString, -1)
if saved then
tellplayer(source,'Saved!', 'Area saved as', name)
@DemmyDemon
DemmyDemon / dumpsterfrob.lua
Created July 25, 2023 21:03
When you really need to frob some dumpsters
local dumpsterModels = {
218085040,
}
function Frob(dumpster)
end
function MaybeFrob(candidate)
if not DoesEntityExist(candidate) then return end
local model = GetEntityModel(candidate)
@DemmyDemon
DemmyDemon / pedcycler.lua
Last active July 24, 2023 19:32
Just helping a donkey entertain their kid!
-- Here we list our ped models we want to cycle through.
local pedCycle = {
"s_m_y_fireman_01",
"s_m_y_hwaycop_01",
"s_m_y_marine_03",
"s_m_y_swat_01",
"s_m_m_fibsec_01",
"s_m_m_paramedic_01",
"s_f_y_scrubs_01",
"g_m_m_chicold_01",
@DemmyDemon
DemmyDemon / near-atm.lua
Created July 23, 2023 21:31
The question "How can I check if I'm near an ATM?" comes up a lot in FiveM scripting chat.
local maxDistance = 1.0
local IsNear = false
AddEventHandler('near-atm-distance', function(distance)
maxDistance = distance
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
@DemmyDemon
DemmyDemon / superman_client.lua
Created August 19, 2020 19:35
The event I use for the replication of "cloudfall", except you can actually fly *up*
RegisterNetEvent('night-tools:superman')
AddEventHandler ('night-tools:superman', function()
serverlog('is flying!')
SetPlayerParachutePackTintIndex(PlayerId(),3)
SetPlayerParachuteTintIndex(PlayerId(),6)
Citizen.CreateThread(function()
GiveWeaponToPed(PlayerPedId(),`GADGET_PARACHUTE`,1,false,true)