[HELP] I need help for my inventory script

Hello,
i’m creating a custom framework and i need help to send information from client.lua to script.js

local items = {
    {['itemid'] = 1, ['label'] = 'Pistola', ['weight'] = 2, ['icon'] = 'pistol.png', ['stackable'] = true, ['information'] = 'test item'}
}

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(10)

		if IsControlJustReleased(0, 311) then
			SetNuiFocus(true, true)

			SendNUIMessage({
				command = 'openInventory',
				itemlist = items
			})

			TriggerServerEvent('kr-inventory:checkFirstInventory')
		end

	end
end)

script.js


var itemList

$(document).ready(function() {
	window.addEventListener('message', function(event) {
		var item = event.data

		if (item.command == 'openInventory') {
			$('.primaryWrap').empty()
			$('.secondaryWrap').empty()
			$('.primaryWeight').empty()
			$('.secondaryWeight').empty()
			$('.primaryInventoryName').empty()
			$('.secondaryInventoryName').empty()
			$('#draggedItem').empty()
			$('#search-text').val('')
			$('.inventory').fadeIn(100)
			$('.options').fadeIn(100)
			$('.background').fadeIn(100)

			searchActive = false
			allowMouseUp = false
			currentlyDragging = false
			currentDragId = null
			primaryName = null
			secondaryName = null
			primaryWeight = 0
			primaryWeightLimit = 0
			secondaryWeight = 0
			secondaryWeightLimit = 0
			itemList = item.itemlist
		} else if (item.command == 'primaryInventoryData') {
			primaryName = item.name
			primaryWeightLimit = item.primaryWeightLimit

			$('.primaryInventoryName').append(primaryName)

			var inventoryData = item.inventoryData

			for (i = 1; i <= item.maxSlots; i++) {
				$('.primaryWrap').append("<div id='primarySlot" + i + "' class='item'></div>")
			}
			
			for (x in inventoryData) {
				if (inventoryData[x]) {
					var slot = parseInt(x)
					var itemid
					var metadata

					for (y in inventoryData[x]) {
						itemid = inventoryData[x][y]['itemid']
						metadata = inventoryData[x][y]['information']
						break
					}

					if (itemid) {
						var amount = inventoryData[x]['amount']
						var weight = itemList[itemid]['weight']
						var label = itemList[itemid]['label']
						var icon = itemList[itemid]['icon']
						var stackable = itemList[itemid]['stackable']
						var amountDisplay = ''

						if (amount > 1) {
							amountDisplay = amount + 'x '
						}

						$('#primarySlot' + slot).append("<div class='itemname'> " + amountDisplay + label + " </div> <div class='information'> " + (weight * amount).toFixed(1) + " </div></div> <img src='icons/" + icon + "' data-label='" + label + "' data-itemid='" + itemid + "' data-metadata='" + JSON.stringify(metadata) + "' data-currentslot='" + slot + "' data-stackable='" + stackable + "' data-amount='" + amount + "' data-weight='" + weight + "' data-inventoryname='" + primaryName + "' class='itemimage'>")

						primaryWeight += weight * amount
					}
				}
			}

			$.post('http://kr-inventory/updateWeight', json.stringify({weight: primaryWeight}))

			UpdateWeights()

can you be more specific on what you need help with?

I need help to send the items list from client to java because now if i put console.log(itemid) in this loop

for (y in inventoryData[x]) {
	itemid = inventoryData[x][y]['itemid'] 
-- here
        console.log(itemid)
	metadata = inventoryData[x][y]['information']
	break
}

i get a nil value in the console

is this undefined too?

the problem is the way in which I have to send the information, if with the parentheses if with the json or other

Try console.log(item.itemlist) from js. Is that also a nil value?

Solved with “{}”

1 Like