Jump to content

[HELP] DX functions


Erknneto

Recommended Posts

Hello everyone. I need some help here.
I wan't to create a DX Inventory for my server, with some buttons.
I created a script that creates the inventory but only 1 button is working.

local newInvItems = {
{"Assault Rifle"},
{"AK-47"},
{"Sniper"},
{"Pump Shotgun"},
{"Crossbow"},
{"Custom SMG"},
{"Shotgun"},
{"Semi-Automatic Pistol"},
{"Machete"},
{"5.56 Rifle Ammo"},
{"Sniper Rifle Ammo"},
{"12 Gauge"},
{"Pistol Bullet"},
{"Wooden Arrow"},
{"SMG Ammo"},
{"Apple"},
{"Banana"},
{"Orange"},
{"Tomato"},
{"Cereal Box"},
{"Juice Box"},
{"Milk Bottle"},
{"Water Bottle"},
{"Raw Meat"},
{"Cooked Meat"},
{"Battery"},
{"Gallon (Fuel)"},
{"Gallon (Empty)"},
{"Bottle (Empty)"},
{"Medical Satchel"},
{"Rags"},
{"Cloth"},
{"Gun Powder"},
{"Alcohol Bottle"},
{"Wood"},
{"Metal"},
{"Stone"},
{"Building Tool"},
{"Fishing Rod"},
{"Cannabis Seed"},
{"Cannabis Medicinal"},
{"Sulphur"},
{"Charcoal"},
{"Axe"},
{"Pickaxe"},
{"C4"},
{"Lock"},
{"Map"},
{"GPS"},
{"Watch"},
{"Toolbox"},
{"Medium Backpack"},
{"Large Backpack"},
{"Top Hat"},
{"Witch Hat"},
{"Horn Hat"},
{"Pizza Hat"},
{"Police Hat"},
{"SWAT Hat"},
{"SWAT Armour"},
{"Chicken Head"},
{"Gas Mask"},
{"Eyepatch"},
{"Motorcycle Helmet"},
}

local screenW, screenH = guiGetScreenSize()
local inventoryLimit = 28
showCursor(true)

function renderInventory ()
	mainTable = dxDrawRectangle(10, (screenH - 583) / 2, 260, 598, tocolor(0, 0, 0, 170), false)
	lootTable = dxDrawRectangle(10, (screenH - 670) / 2, 260, 38, tocolor(0, 77, 126, 150), false)
	rowNumber = 0
	for i,item in ipairs(newInvItems) do
		if ( getElementData(getLocalPlayer(),item[1]) >= 1 ) then
			if not ( rowNumber > inventoryLimit ) then
				rowNumber = rowNumber + 1
				itemBackground = dxDrawRectangle(20, 85+rowNumber*20, 185, 15, tocolor(0, 77, 126, 150), false)
				takeBackground = dxDrawRectangle(210, 85+rowNumber*20, 50, 15, tocolor(0, 77, 126, 150), false)
				itemText = dxDrawText(item[1].." ("..getElementData(getLocalPlayer(),item[1]).."x)",25,85+rowNumber*20,tocolor(255,255,255, 255))
				takeText = dxDrawText("Take",221,85+rowNumber*20,tocolor(255,255,255, 255))
			end
		end
	end
end
addEventHandler("onClientRender", root, renderInventory)

function inventoryClickDetect (button,state,x,y)
	if ( button == "left" ) then
		if ( state == "down" ) then
			if ( x >= 210 and x <= 210 + 50 and y >= 85+rowNumber*20 and y <= 85+rowNumber*20 + 15 ) then 
				outputChatBox("Test!") 
			end 
		end
	end
end
addEventHandler("onClientClick",root,inventoryClickDetect)

 

It creates all buttons and all things, but only the last button created is working.
How can I make for every button to work?


sorry for my bad english, I'm brazilian :P

Link to comment

You needed to loop the click function for all the buttons.

function inventoryClickDetect (button,state,x,y)
	for i=1,rowNumber do
		if ( button == "left" ) then
			if ( state == "down" ) then
				if ( x >= 210 and x <= 210 + 50 and y >= 85+i*20 and y <= 85+i*20 + 15 ) then 
					outputChatBox("Took "..newInvItems[i][1])
					return
				end 
			end
		end
	end
end
addEventHandler("onClientClick",root,inventoryClickDetect)

 

Edited by Mr.Loki
  • Thanks 1
Link to comment

Thank you! The buttons are working now!

But I need one more thing, can you help me with the button getting the right item? It appears on the chat for example "Took Assault Rifle" in the "Map" button. How can I fix it so it can take the Map instead of the Assault Rifle, or like the GPS instead of the AK-47?

Here is a screenshot of what I'm trying to do

d11VWq_MQJOespCbubSnZQ.png

 

And here is the new code:
 

local newInvItems = {
{"Assault Rifle"},
{"AK-47"},
{"Sniper"},
{"Pump Shotgun"},
{"Crossbow"},
{"Custom SMG"},
{"Shotgun"},
{"Semi-Automatic Pistol"},
{"Machete"},
{"5.56 Rifle Ammo"},
{"Sniper Rifle Ammo"},
{"12 Gauge"},
{"Pistol Bullet"},
{"Wooden Arrow"},
{"SMG Ammo"},
{"Apple"},
{"Banana"},
{"Orange"},
{"Tomato"},
{"Cereal Box"},
{"Juice Box"},
{"Milk Bottle"},
{"Water Bottle"},
{"Raw Meat"},
{"Cooked Meat"},
{"Battery"},
{"Gallon (Fuel)"},
{"Gallon (Empty)"},
{"Bottle (Empty)"},
{"Medical Satchel"},
{"Rags"},
{"Cloth"},
{"Gun Powder"},
{"Alcohol Bottle"},
{"Wood"},
{"Metal"},
{"Stone"},
{"Building Tool"},
{"Fishing Rod"},
{"Cannabis Seed"},
{"Cannabis Medicinal"},
{"Sulphur"},
{"Charcoal"},
{"Axe"},
{"Pickaxe"},
{"C4"},
{"Lock"},
{"Map"},
{"GPS"},
{"Watch"},
{"Toolbox"},
{"Medium Backpack"},
{"Large Backpack"},
{"Top Hat"},
{"Witch Hat"},
{"Horn Hat"},
{"Pizza Hat"},
{"Police Hat"},
{"SWAT Hat"},
{"SWAT Armour"},
{"Chicken Head"},
{"Gas Mask"},
{"Eyepatch"},
{"Motorcycle Helmet"},
}

local screenW, screenH = guiGetScreenSize()
local inventoryLimit = 28
local playerItems = {}
inventoryShowing = false

function renderInventory ()
	mainTable = dxDrawRectangle(10, (screenH - 583) / 2, 260, 598, tocolor(0, 0, 0, 170), false)
	lootTable = dxDrawRectangle(10, (screenH - 670) / 2, 260, 38, tocolor(0, 77, 126, 150), false)
	takeText = dxDrawText("Loot (0/24)",25, (screenH - 644) / 2, 260, 38,tocolor(255,255,255, 255))
	rowNumber = 0
	for i,item in ipairs(newInvItems) do
		if ( getElementData(getLocalPlayer(),item[1]) >= 1 ) then
			if not ( rowNumber > inventoryLimit ) then
				rowNumber = rowNumber + 1
				itemBackground = dxDrawRectangle(20, 85+rowNumber*20, 185, 15, tocolor(0, 77, 126, 150), false)
				takeBackground = dxDrawRectangle(210, 85+rowNumber*20, 50, 15, tocolor(0, 77, 126, 150), false)
				itemText = dxDrawText(item[1].." ("..getElementData(getLocalPlayer(),item[1]).."x)",25,85+rowNumber*20,tocolor(255,255,255, 255))
				takeText = dxDrawText("Take",221,85+rowNumber*20,tocolor(255,255,255, 255))
			end
		end
	end
end

function inventoryClickDetect (button,state,x,y)
	for i=1,rowNumber do
		if ( button == "left" ) then
			if ( state == "down" ) then
				if ( inventoryShowing ) then
					if ( x >= 210 and x <= 210 + 50 and y >= 85+i*20 and y <= 85+i*20 + 15 ) then 
						outputChatBox("Took "..newInvItems[i][1])
						return
					end 
				end
			end
		end
	end
end

function showInventory ()
	if ( inventoryShowing ) then
		removeEventHandler("onClientRender", root, renderInventory)
		removeEventHandler("onClientClick",root,inventoryClickDetect)
		inventoryShowing = false
		showChat(true)
		showCursor(false)
	else
		addEventHandler("onClientRender", root, renderInventory)
		addEventHandler("onClientClick",root,inventoryClickDetect)
		inventoryShowing = true
		showChat(false)
		showCursor(true)
	end
end
bindKey("K","down",showInventory)

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...