Create a Custom Action

How to create new actions for the NPCs

First of all, you need to create a new function in the file:

`custom/client/actions.lua`

All the actions must include three different input parameters:

  • networkId: It's the PED network ID to identify the entity and synchronize to all players

  • playerIdentifier: It's the original player identifier who trigger the interaction with the NPC

  • hasWeapon: It's a true/false parameter that show if the player who interacted with the NPC was carrying a weapon or not

function CustomFollow(networkId, playerIdentifier, hasWeapon)
    -- Transform the network ID into local Ped
    local targetPed = NetToPed(networkId)
    -- Validates that player is carrying a weapon and code is only executed to the player who trigger the action
    if hasWeapon and playerIdentifier == GetClientIdentifier() then
        -- Perform the action
        TaskFollowToOffsetOfEntity(targetPed, PlayerPedId(), 1.0, -1.0, 0.0, 5.0, -1, 2.0, true)
    end
end

Additionally, you can add some server side functions just in case your new action implementation requires server side logic, this has to be implemented in the following file:

`custom/server/actions.lua`

The server-side action can be something like this (it's the build-in robbery action):

RegisterNetEvent("sse_npc_interaction:robberySuccess", function(pedNetworkID)
    local src = source
    local currentTime = os.time()

    if not Robberies[pedNetworkID] or currentTime - Robberies[pedNetworkID] > Config.NPCRobberyTimeout then
        for itemName, itemValue in pairs(Config.NPCRobberyItems) do
            -- Roll a random number between 1 and 100
            local roll = math.random(1, 100)
            if roll <= itemValue.probability then
                AddItem(GetPlayer(src), itemName, math.random(itemValue.min, itemValue.max))
            end
        end
        Robberies[pedNetworkID] = currentTime
    else
        TriggerClientEvent("sse_npc_interaction:clientNotification", src, Translations["title"], Translations["action-npc-robbery-already-robbed"], "info")
    end
end)

Once you have completed the development of your new action, you need to add the keyword which will be triggering this action. This needs to be done in the following file:

You need to add a new entry in the CUSTOM_KEYWORDS list:

Where the key of the new entry is the keyword that needs to be detected based on the conversation with the NPC (it's done via the AI), and the value should be the function name you have implemented in the custom/client/actions.lua file.

Last updated