TriggerServerEvent does not exist in the current context

Hi there everyone!

I’m trying to execute a lua function in a C# resource and was directed to this wiki page and I’ve done my best to follow it.

First, I registered the server event in a server script.

server/main.lua:

RegisterServerEvent("makepmnt")
AddEventHandler("makepmnt", function(amnt)
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	xPlayer.addMoney(amnt)
end)

Then, following the wiki guidelines, I added the C# line:

TriggerServerEvent("makepmnt", "5000");

But received:

What am I doing wrong? I don’t see anything I’m missing according to the wiki page.

Thanks for your time!

Did you properly add in the .dll references? Follow this: [How-to] Setting up C# and creating a basic resource

Hi there Briglair,

The resource is this one provided by Scammer. All aspects of it work great, the only thing I’d like to change is to utilize ESX payments. I’ve edited other elements of the dll successfully(like adding missions, changing notification elements) I’m just not able to manage this single thing.

I see elsewhere in his code:

BaseScript.TriggerEvent

Is that how I should be structuring it?

Is the CitizenFX.Core reference properly added.

Because I tested it and it worked for me,

Try doing BaseScript.TriggerServerEvent(...)

It could be that the class you are using isn’t inheriting it.

That does get rid of the error but unfortunately the function doesn’t work. I’ll post in the ESX to see if someone can help me dig my way out of my week-long effort to accomplish this.

Thanks for your time!

Add : BaseScript after the name of your class to inherit it.

Like this?

RegisterServerEvent("makepmnt.BaseScript")
AddEventHandler("makepmnt", function(amnt)
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	xPlayer.addMoney(amnt)
end)

Sorry for the ignorance. I’m brand new to this.

I mean in the C# code. In the guide I linked above, for an example, look under this section of the guide:

Creating a basic resource

1) Basic script

Since the rest of the resource is successfully using BaseScript.TriggerEvent in the code, that would have already been taken care of, no? I’ve only tried to add a single line of code in the dll, the rest of the changes being additions I noted to the server/main.lua.

Each class you want to use FiveM methods in would need to derive from BaseScript. Where exactly in scammer’s code are you trying to put the trigger?

here’s the mission’s cs file:

using CitizenFX.Core;
using CitizenFX.Core.Native;
using CitizenFX.Core.UI;
using Freeroam.Utils;
using System;
using System.Threading.Tasks;

namespace Freeroam.Missions
{
    internal class Gang46
    {
        public Ped targetPed;
        public Ped[] bodyguards;

        public Gang46(Ped targetPed, Ped[] bodyguards)
        {
            this.targetPed = targetPed;
            this.bodyguards = bodyguards;
        }
    }

    class GangFight46 : IMission
    {
        private static Vector3[] targetSpawns = new Vector3[]
        {
            new Vector3(3800.76f, 4475.42f, 5.99f),
            new Vector3(3817.35f, 4482.64f, 6.17f),
            new Vector3(3822.63f, 4467.38f, 3.16f),
            new Vector3(3865.33f, 4463.55f, 2.72f),
            new Vector3(3818.56f, 4441.96f, 2.81f),
            new Vector3(3811.93f, 4447.48f, 3.56f),
            new Vector3(3800.64f, 4441.82f, 4.25f),
            new Vector3(3824.62f, 4474.13f, 5.16f),
            new Vector3(3812.00f, 4490.71f, 5.99f)

        };

        private Gang46[] targets = new Gang46[9];
        private bool enableTick = false;

        public async void Start()
        {
            Random random = new Random();
            for (int i = 0; i < targets.Length; i++)
            {
                Ped targetPed = await Util.CreatePed(PedHash.Gaffer01SMM, targetSpawns[i]);
                targetPed.Weapons.Give(WeaponHash.CarbineRifle, int.MaxValue, true, true);
                targetPed.Task.StartScenario("WORLD_HUMAN_AA_SMOKE", targetPed.Position);
                Ped[] bodyguards = await SpawnBodyguards(targetPed, random.Next(0, 0));

                Function.Call(Hash.FLASH_MINIMAP_DISPLAY);

                Blip blip = targetPed.AttachBlip();
                blip.Sprite = BlipSprite.Enemy;
                blip.Color = BlipColor.TrevorOrange;
                blip.Name = Strings.MISSIONS_GANGFIGHT46_BLIP;
                blip.Scale = 0.8f;

                targets[i] = new Gang46(targetPed, bodyguards);
            }

            Util.DisplayHelpText(Strings.MISSIONS_GANGFIGHT46_INFO);
            Screen.ShowSubtitle(Strings.MISSIONS_GANGFIGHT46_START, 15000);
            enableTick = true;
        }

        public void Stop(bool success)
        {
            if (!success)
            {
                foreach (Gang46 target in targets)
                {
                    if (target != null)
                    {
                        DespawnTargetSquad(target);
                    }
                }
            }
            else
            {
                Screen.ShowNotification(Strings.MISSIONS_GANGFIGHT46_ALLTARGETSKILLED);
                //BaseScript.TriggerEvent(Events.MONEY_ADD, 5000);
                //BaseScript.TriggerEvent(xPlayer.addMoney(5000));
                BaseScript.TriggerServerEvent("makepmnt", 5000);
                //xPlayer.addMoney(5000);
                BaseScript.TriggerEvent(Events.XP_ADD, 30);
            }
        }

        public async Task Tick()
        {
            if (enableTick)
            {
                Ped playerPed = Game.PlayerPed;
                if (Game.Player.WantedLevel > 0) Game.Player.WantedLevel = 0;
                if (playerPed.IsDead)
                {
                    BaseScript.TriggerEvent(Events.MISSION_STOP, false);
                    enableTick = false;
                }
                else
                {
                    int livingTargets = 0;
                    for (int i = targets.Length - 1; i > -1; i--)
                    {
                        if (targets[i] != null)
                        {
                            if (!targets[i].targetPed.IsDead) livingTargets++;
                            else
                            {
                                Entity killer = targets[i].targetPed.GetKiller();

                                if (killer == playerPed)
                                {
                                    //if (Game.Player.WantedLevel < 3) Game.Player.WantedLevel = 3;
                                }

                                DespawnTargetSquad(targets[i]);
                                targets[i] = null;
                            }
                        }
                    }

                    if (livingTargets == 0) BaseScript.TriggerEvent(Events.MISSION_STOP, true);
                }
            }

            await Task.FromResult(0);
        }

        private async Task<Ped[]> SpawnBodyguards(Ped targetPed, int amount)
        {
            PedGroup group = new PedGroup();
            group.Add(targetPed, true);
            group.FormationType = FormationType.Default;
            group.SeparationRange = 1f;

            RelationshipGroup relationship = World.AddRelationshipGroup("_ASSASSIN_TARGETS");
            relationship.SetRelationshipBetweenGroups(new RelationshipGroup(Util.GetHashKey("COP")), Relationship.Respect, true);
            relationship.SetRelationshipBetweenGroups(new RelationshipGroup(Util.GetHashKey("SECURITY_GUARD")), Relationship.Respect, true);
            targetPed.RelationshipGroup = relationship;

            Random random = new Random();
            Ped[] bodyguards = new Ped[amount];
            for (int i = 0; i < amount; i++)
            {
                float x = Util.GetRandomFloat(random, -2, 2);
                float y = Util.GetRandomFloat(random, -2, 2);
                Vector3 spawnPos = targetPed.GetOffsetPosition(new Vector3(x, y, 0f));
                Ped bodyguard = await Util.CreatePed(PedHash.FibOffice01SMM, spawnPos);
                bodyguard.Armor = 300;
                bodyguard.Weapons.Give(WeaponHash.CarbineRifle, int.MaxValue, true, true);

                bodyguard.RelationshipGroup = new RelationshipGroup(Util.GetHashKey("SECURITY_GUARD"));
                group.Add(bodyguard, false);

                bodyguards[i] = bodyguard;
            }

            return bodyguards;
        }

        private void DespawnTargetSquad(Gang46 target)
        {
            target.targetPed.AttachedBlip.Delete();
            target.targetPed.MarkAsNoLongerNeeded();
            foreach (Ped bodyguard in target.bodyguards) bodyguard.MarkAsNoLongerNeeded();
        }
    }
}

In that file, starting at line 81, you can see where I start trying different things:

                Screen.ShowNotification(Strings.MISSIONS_GANGFIGHT46_ALLTARGETSKILLED);
                //BaseScript.TriggerEvent(Events.MONEY_ADD, 5000);
                //BaseScript.TriggerEvent(xPlayer.addMoney(5000));
                BaseScript.TriggerServerEvent("makepmnt", 5000);
                //xPlayer.addMoney(5000);
                BaseScript.TriggerEvent(Events.XP_ADD, 30);

During my searches and forum topics, I was directed toward calling a lua function, so my alteration to the .cs file is this:

Comment his line:

BaseScript.TriggerEvent(Events.MONEY_ADD, 5000);

And add my line:

 BaseScript.TriggerServerEvent("makepmnt", 5000);

and in server/main.lua, I’ve got this:

ESX = nil

TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

RegisterServerEvent("makepmnt")
AddEventHandler("makepmnt", function(amnt)
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	xPlayer.addMoney(amnt)
end)

and just in case it’s important, my _resource.lua:

resource_manifest_version 'f15e72ec-3972-4fe4-9c7d-afc5394ae207'

client_scripts {
	"client/main.lua",
	"nativeui.net.dll",
	"Freeroam.net.dll",
}

server_scripts {
	"server/main.lua",
	"server/missions.lua",
	"server/notifications.lua"
}

So, all I’ve done so far is to comment his payment line out and add mine in the .cs file and to register the server event in my server LUA script.

Hm… that event should be triggered just fine. Are you able to add in a print() with some text inside the event on the server to make sure it is actually being triggered?

Is it just the money isn’t being paid or something?

As far as ESX goes, I’m unsure. I haven’t used it, and you should ask in their topics about that, as you said.

Hi Briglair,

I tried adding a print:

RegisterServerEvent("makepmnt")
AddEventHandler("makepmnt", function(amnt)
	print("hello, world!")
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	xPlayer.addMoney(amnt)
end)

But I didn’t see it pop up anywhere onscreen. I looked in F8 and around the UI. Am I looking in the wrong place or does this mean that the event is not being triggered?

hi there Briglair,

I tried a ton of stuff without success but after a while, tried hardcoding the amount, which caused the event to work successfully. With that, I thought maybe amnt is a protected variable so I changed it to frAmnt and it’s working successfully still.

Is it likely that was the problem or did I just fumble into changing something else that would have fixed it?

I can’t thank you enough for taking the time to help me. This has been a pretty brutal learning process.

It could be, but I don’t see that as a reserved word for Lua. I’m not sure… lol. It could have been something else. If it works, it works!

It always is :stuck_out_tongue:

1 Like