Here’s a collection of scripts I’ve learned how to create / accumulated over the course of my time here at FiveM.
I wont be providing troubleshooting support for these scripts, however if you have any questions let me know. Enjoy.
Raycasting in a circle around the player:
This is less useful now that we have built in natives, but it might help someone. You should be able to use raycasts fine instead of using 3D Raycasts (AKA Spheres) just by replacing the StartShapeTestCapsule call to the raycast native. This is just more accurate / consistent.
angleint = 0 --Storing the last angle we were at
function CapsuleCheckForNearbyPed(inputped)
x, y, z = table.unpack(GetEntityCoords(inputped, true))
flag = 12 --Type of entity
radius = 60 --Radius of Capsule Casted
Wait(7) -- Timer so it's not firing off every tick.
for i = angleint, 72 do
angleint = angleint + 1
AdjustAngleInt()
local angle = math.rad(i * 5)
local startX = (60.0 * math.cos(angle)) + x;
local startY = (60.0 * math.sin(angle)) + y;
local endX = x - (startX - x)
local endY = y - (startY - y)
ray = StartShapeTestCapsule(startX,startY,z,endX,endY,z,radius,flag,inputped,7)
_, _, _, _, result = GetShapeTestResult(ray)
return result
end
end
-- normally you wouldn't set it up this way but the variable is global so it doesn't matter.
function AdjustAngleInt()
if angleint > 72 then
angleint = 1
end
end
Detect NPC by Aiming at them.
Note, this can also be used to detect any entity, I just so happen to filter out for only PEDs.
function DetectNpcByAiming()
local aiming = false
local entity
if IsPlayerFreeAiming(PlayerId()) then
aiming, entity = GetEntityPlayerIsFreeAimingAt(PlayerId())
if (aiming) then
if IsEntityAPed(entity) then
return entity
end
end
end
end
Populating Local Ped index
This one was provided by the Elements, they added it just after I managed to solve my Raycasting problems haha! Thanks again
pedindex = {} -- Define a global table to store them in.
function PopulatePedIndex()
local handle, ped = FindFirstPed()
local finished = false -- FindNextPed will turn the first variable to false when it fails to find another ped in the index
repeat
if not IsEntityDead(ped) then
pedindex[ped] = {}
end
finished, ped = FindNextPed(handle) -- first param returns true while entities are found
until not finished
EndFindPed(handle)
end
Distance Between 2 sets of Coords
Pass it two entities, it’ll calculate the distance between the two.
function DistanceBetweenCoords(ent1, ent2)
local x1,y1,z1 = table.unpack(GetEntityCoords(ent1, true))
local x2,y2,z2 = table.unpack(GetEntityCoords(ent2, true))
local deltax = x1 - x2
local deltay = y1 - y2
local deltaz = y1 - y2
dist = math.sqrt((deltax * deltax) + (deltay * deltay) + (deltaz * deltaz))
xout = math.abs(deltax)
yout = math.abs(deltay)
zout = math.abs(deltaz)
result = {distance = dist, x = xout, y = yout, z = zout}
return result
end
Highlight Object
Just draws a rectangle around an object, can be any entity really. I specifically took the example from the nativesdb but they didn’t include the “RequestedStreamedTextureDict” segment which is essential.
function HighlightObject(object)
x, y, z = table.unpack(GetEntityCoords(object, true))
SetDrawOrigin(x, y, z, 0)
RequestStreamedTextureDict("helicopterhud", false)
DrawSprite("helicopterhud", "hud_corner", -0.01, -0.01, 0.006, 0.006, 0.0, 255, 0, 0, 200)
DrawSprite("helicopterhud", "hud_corner", 0.01, -0.01, 0.006, 0.006, 90.0, 255, 0, 0, 200)
DrawSprite("helicopterhud", "hud_corner", -0.01, 0.01, 0.006, 0.006, 270.0, 255, 0, 0, 200)
DrawSprite("helicopterhud", "hud_corner", 0.01, 0.01, 0.006, 0.006, 180.0, 255, 0, 0, 200)
ClearDrawOrigin()
end
Extra fluff
I didn’t make these, both were from Stack Exchange.
- Get table length. Pass it a table, it returns an integer that gives you its length. Not all tables are indexed equally you’ll come to find when you’re doing the indexing with your own values
function GetTableLength(temptable)
local count = 0
for _ in pairs(temptable) do
count = count+1
end
return count
end
- Rounding a number up from a floating point to an int. I use this in Moneydrop.
function RoundNumber(num, numDecimalPlaces)
if numDecimalPlaces and numDecimalPlaces>0 then
local mult = 10^numDecimalPlaces
return math.floor(num * mult + 0.5) / mult
end
return math.floor(num + 0.5)
end
I’ll update if I have anything notable to add. Most stuff I have is to be fair very niche, but if I find anything else I’ll be sure to post them. Hope they help someone out.