[C#] Inventory system

Hello All,

I have created a plugin that allows a user to add 5 items to their “inventory” and then recall it… BUT is only works on a local basis… I.E… If a user (1) adds 5 items and then does /search 1 it will recall the items but if another person on the server does /search 1 then it wont work…

I believe the problem is that the main chunk of code which saves the information is client side instead of server side… I have tried switching the code around so the script is server side instead but then everything breaks…

After many hours researching i still cannot find the answer i am looking for… So my questions are;

Can you request information from a client and update a server script?
Does the server script need to be in LUA or can it be in a .dll format?

Client side [C#];

if (Game.Player.Character.Model.IsLoaded == true)
                {
                    Dictionary<string, string[]> inventoryDict = new Dictionary<string, string[]>();
                    int pID = CitizenFX.Core.Native.API.GetPlayerServerId(Game.Player.Handle);
                    string ServerID = Convert.ToString(pID);

                    string PlayerHandle = Convert.ToString(Game.Player.ServerId);
                

                    EventHandlers["PlayerSearch:AddItem"] += new Action<string>((string message) =>
                    {
                        string[] items = message.Split(',');

                        if (message != null)
                        {

                            inventoryDict.Add(ServerID, new string[5] { items[0], items[1], items[2], items[3], items[4] });
                            TriggerEvent("chatMessage", "Inventory", new int[] { 255, 255, 0 }, items[0] + ", " + items[1] + ", " + items[2] + ", " + items[3] + " and " + items[4] + " have been added to your inventory" + " {" + ServerID + "} ");
                        }
                    });

Server side [LUA]

AddEventHandler('chatMessage', function(source, name, message)
    cm = stringsplit(message, " ")
	if(cm[1] == "/additem") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:AddItem", source, table.concat(cm, " "))
    end
	if(cm[1] == "/removeitem") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:ClearItem", source, table.concat(cm, " "))
    end
	if(cm[1] == "/search") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:Search", source, table.concat(cm, " "))
    end
end)
AddEventHandler('chatMessage', function(source, name, message)
    cm = stringsplit(message, " ")
	if(cm[1] == "/additem") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:AddItem", source, table.concat(cm, " "))
    end
	if(cm[1] == "/removeitem") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:ClearItem", source, table.concat(cm, " "))
    end
	if(cm[1] == "/search") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:Search", table.concat(cm, " "))
    end
end)

You are passing source which is the clients server id. You need to pass the id that you are typing to where your source is. That works for the additem and removeitem but when you search its only gonna search your local player.

I gave that a go, unfortunately when you remove the source from the last if statement it breaks the plugin -.- how very annoying! Im trying to create a version that only uses c# dll files for the server and client script… see if that will work better, shame there isnt a wiki covering c# functions

I mean Lua does just fine client sided. They will both have the same outcome.

So instead of source, it needs to be cm[2] which will be the clients server id number? as you type in the chat “/search 2”

Yeah you are triggering a client event on the source and it needs to be the player you are trying to trigger the search function on.

I have tried the below code but it still does not work… I’ve also tried both server side and client side in .dll format as well but that doesn’t work either -.-

[LUA] (Whole server side script)

AddEventHandler('chatMessage', function(source, name, message)
    cm = stringsplit(message, " ")
	if(cm[1] == "/additem") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:AddItem", source, table.concat(cm, " "))
    end
	if(cm[1] == "/removeitem") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:ClearItems", source, table.concat(cm, " "))
    end
	if(cm[1] == "/search") then
		CancelEvent()
		table.remove(cm, 1)	
		TriggerClientEvent("PlayerSearch:Search", cm[2], table.concat(cm, " "))
    end
end)



function stringsplit(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={} ; i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        t[i] = str
        i = i + 1
    end
    return t
end