[FREE][ESX] Basic Farming

ESX Farming Script - Fast & Easy Configuration

The FiveM Basic Farming Script is suitable for any ESX server. It offers numerous configuration options that allow users to customize the script according to their specific needs.


GitHub Download

Features:

  • Farming:
    • create an infinite amount of farming position
    • supports every type of items
    • use custom animations for more immersion
    • highly customizable
  • Selling:
    • create shops wherever you want
    • supports every item
  • Dependencies:
    • ESX Legacy

Credits: Thanks to ImBaphomettt for providing the awesome RageUI Library!

YouTube Preview (it’s in German but its still good as a visual showcase, just let me know if you want a shorter and no-commentary preview)

Code is accessible Yes
Subscription-based No
Lines (approximately) 250
Requirements ESX Legacy
Support Yes
4 Likes

really nice work

1 Like

Thanks!

Could be way better in terms of optimization… For example, use RegisterKeyMapping instead of IsControlJustPressed, or rewrite things that runs on every tick like :

Citizen.CreateThread(function()
    while true do 
        Citizen.Wait(0)
        for k, v in pairs(Config.Farm.Type) do 
            local playerPed = PlayerPedId()
            local playercoords = GetEntityCoords(playerPed)
            local x, y, z = table.unpack(v.coords)
            local distance = Vdist(playercoords, x, y, z)
            if distance < 12 then 
                ShowHelp("DrĂĽcke ~INPUT_CONTEXT~ um ~y~" ..v.Label.. "~w~ zu farmen", true)
                if IsControlJustPressed(0, 38) then
                    Notify("Du sammelts jetzt ~y~" ..v.Count.. "~w~ " ..v.Label)
                    local ped = PlayerPedId()
                    TaskStartScenarioInPlace(ped, v.Anim, 0, true)
                    Citizen.Wait(v.Time)
                    ClearPedTasksImmediately(ped)
                    PlaySound(-1, "PICK_UP", "HUD_FRONTEND_DEFAULT_SOUNDSET", 0, 0, 1)
                    PlaySoundFrontend(-1, "MEDAL_GOLD", "HUD_AWARDS", 0);
                    Notify("Du hast " ..v.Count.. " ~g~" ..v.Label.. "~w~ Gesammelt")
                    local item = v.Value 
                    local count = v.Count
                    TriggerServerEvent("gmw_farm:giveItem", item, count)
                end 
            end
        end
    end
end)

To :

CreateThread(function()

    local playerPed = PlayerPedId()
    local playercoords = GetEntityCoords(playerPed)
    local x, y, z = table.unpack(v.coords)
    local distance = Vdist(playercoords, x, y, z)

    while true do 
            local sleep = 5000
        for k, v in pairs(Config.Farm.Type) do 
            
            if distance < 12 then 
                sleep = 0
                ShowHelp("DrĂĽcke ~INPUT_CONTEXT~ um ~y~" ..v.Label.. "~w~ zu farmen", true)
                if IsControlJustPressed(0, 38) then
                    Notify("Du sammelts jetzt ~y~" ..v.Count.. "~w~ " ..v.Label)
                    local ped = PlayerPedId()
                    TaskStartScenarioInPlace(ped, v.Anim, 0, true)
                    Citizen.Wait(v.Time)
                    ClearPedTasksImmediately(ped)
                    PlaySound(-1, "PICK_UP", "HUD_FRONTEND_DEFAULT_SOUNDSET", 0, 0, 1)
                    PlaySoundFrontend(-1, "MEDAL_GOLD", "HUD_AWARDS", 0);
                    Notify("Du hast " ..v.Count.. " ~g~" ..v.Label.. "~w~ Gesammelt")
                    local item = v.Value 
                    local count = v.Count
                    TriggerServerEvent("gmw_farm:giveItem", item, count)
                end 
            end
        end
        Wait(sleep)
    end
end)

Hey, thanks for the feedback! While you’re right about the fact, that the script isn’t optimized, since it was one of my first ever scripts I wrote, the code you provided has errors, like that the distance isn’t recalculated. If you want to contribute to make this project a better, more optimized and safer script I invite you to create a pull request!

What do you mean with

like that the distance isn’t recalculated.

?? Like I’ve done, when you are <12 then it runs, else it waits = less consumption

And Local must be outside the loop

So first of all your code is trying to calculate the distance between the player and a given coordinate before the coordinate is read in the foreach loop. This alone is null-value that you are trying to read. But if we assume that the coords are read properly, the variable distance is calculated once in your thread. Thus staying the same throughout the loop. An other problem in your example would be that the help notification would still be flickering if one of the distances is less than 12, because it is in a for-loop.
This would be my implementation of a potential fix. I’ll test it later and push it to the main branch if it works but in theory everything should be good:

CreateThread(function()
    local isNearPoint = false
    while true do 
        Wait(0)
        for index, v in pairs(Config.Farm.Type) do 
            
            local ped = PlayerPedId()
            local playerCoords = GetEntityCoords(ped)
            local x, y, z = table.unpack(v.coords)
            local distance = Vdist(playerCoords, x, y, z)

            if distance > 12 then 
                goto continue
            end

            isNearPoint = true
            
            ShowHelp("DrĂĽcke ~INPUT_CONTEXT~ um ~y~" ..v.Label.. "~w~ zu farmen", true)
            
            if IsControlJustPressed(0, 38) then
                local item = v.Value
                local itemLabel = v.Label
                local count = v.Count

                Notify("Du sammelts jetzt ~y~" ..count.. "~w~ " ..itemLabel)
                
                TaskStartScenarioInPlace(ped, v.Anim, 0, true)
                Wait(v.Time)
                ClearPedTasksImmediately(ped)
                
                PlaySound(-1, "PICK_UP", "HUD_FRONTEND_DEFAULT_SOUNDSET", 0, 0, 1)
                PlaySoundFrontend(-1, "MEDAL_GOLD", "HUD_AWARDS", 0);
                
                Notify("Du hast " ..count.. " ~g~" ..itemLabel.. "~w~ Gesammelt")
                
                TriggerServerEvent("gmw_farm:giveItem", item, count)
            end 

            if isNearPoint then
                break
            end
            
            ::continue::
        end

        if not isNearPoint then 
            Wait(1500)
        end

        isNearPoint = false
    end
end)

Let me know if you would change something. Thanks for your feedback.

Using continue is a bad thing, as well as having local inside the loop… Notification won’t flicker with my code as it started with sleep = 0 when distance <12. If you are far, you won’t see the notification