Jump to content

Could somebody help me with a weapon selector and dx functions?


Dzsozi (h03)

Recommended Posts

Hello !

I can't manage to get my weapon selector to work and some dx functions.

I would like to create a custom weapon selector hud that would look like this (I created this image in photoshop):

TS1fpON.png

And it would work like this:

If you scroll your mouse wheel upwards it would change the middle (equipped/current) weapon picture (in this case ak-47) to the next weapon, the upper weapon picture (in this case the shotgun) to your current/equipped weapon and if you press left click it would switch to that weapon. So when you scroll the mouse wheel up or down it would change between the weapon icons and if you press left click it would switch to the weapon you scrolled to. I can't manage to fix the icon changing, it doesn't change the icons when I scroll the wheel, only the ammo text updates. The other problem is that I have to scroll through every weapon slots even if I have only 1 or 2 weapons (except weapon slot 0, the fist) for example. So I could skip the empty weapon slots. Could somebody help me with this please? I would be so grateful!

Here's a download link for the resource: https://www.mediafire.com/?pdi3sdw1o64iu2l

Edited by Dzsozi
Link to comment
local screenWidth, screenHeight = guiGetScreenSize()

local currentWidth, currentHeight = 250, 125
local currentOutline = 4
local gapBetween = 32

local left = (screenWidth/2) - (currentWidth/2)
local top = (screenHeight/2) - (currentHeight/2)

local controls = { "aim_weapon","radio_next","radio_previous", "fire", "vehicle_fire", "vehicle_secondary_fire" }

local kepszam = 0
local weaponUpper = 0
local weaponLower = 0
local mutat = false

local weaponslot = 0

local nextAmmo = nil
local currentAmmo = nil
local previousAmmo = nil

function getPreviousWeapon() 
    local tempSlot = {} 
    local curSlot = getPedWeaponSlot(localPlayer) 
    if (curSlot == 0) and (getPedWeapon(localPlayer) == 0 or getPedWeapon(localPlayer) == 1) then 
        curSlot = 13 
    end 
     
    tempSlot["ID"] = 13 
    repeat 
        tempSlot["ID"] = tempSlot["ID"]-1 
    until (tempSlot["ID"] < curSlot) and not (tempSlot["ID"] ~= 0 and getPedWeapon(localPlayer, tempSlot["ID"]) == 0) 
    return getPedWeapon(localPlayer, tempSlot["ID"]) 
end

function getNextWeapon()
    local tempSlot = {} 
    local curSlot = getPedWeaponSlot(localPlayer) 
    if (curSlot == 12) then 
        curSlot = - 1 
    end 
     
    tempSlot["ID"] = - 1 
    repeat 
        tempSlot["ID"] = tempSlot["ID"] + 1 
        if tempSlot["ID"] == 13 then 
            tempSlot["ID"] = -1 
            curSlot = -1 
        end 
    until (tempSlot["ID"] > curSlot) and not (tempSlot["ID"] ~= 0 and getPedWeapon(localPlayer, tempSlot["ID"]) == 0) 
    return getPedWeapon(localPlayer, tempSlot["ID"]) 
end

function selectWeapon()
	if mutat then
		local previousWeapon = getPreviousWeapon()
		local currentWeapon = getPedWeapon(getLocalPlayer(), getPedWeaponSlot(getLocalPlayer()))
		local nextWeapon = getNextWeapon()
		
		local textx = left + (currentWidth / 2)
		local texty = (top + (currentHeight / 2))
		dxDrawText( "Fegyvereid",textx, texty - 215, textx, texty - 215, tocolor(200,0,100,255), 2, "default", "center", "center", false,false,true)
		
		if weaponslot == getPedWeaponSlot(getLocalPlayer()) then
			dxDrawRectangle ( left - currentOutline, top - currentOutline, currentWidth + currentOutline*2, currentHeight + currentOutline*2, tocolor ( 200, 0, 100, 180 ) )
		end
		dxDrawRectangle ( left, top, currentWidth, currentHeight, tocolor ( 0, 0, 0, 180 ) )
		
		dxDrawRectangle ( left + 26, top + 48 - currentHeight - gapBetween, currentWidth - 52, currentHeight - 32, tocolor ( 0, 0, 0, 180 ) )
		dxDrawRectangle ( left + 26, top - 16 + currentHeight + gapBetween, currentWidth - 52, currentHeight - 32, tocolor ( 0, 0, 0, 180 ) )
		
		
		dxDrawImage(left + 26, top - currentHeight + gapBetween, 256-128, 128-64, "weap/".. nextWeapon ..".png", 0, 0, 0, tocolor(255, 255, 255, 100), true)
		nextAmmo = getPedTotalAmmo(getLocalPlayer(), getSlotFromWeapon(nextWeapon)) - getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(nextWeapon)) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(nextWeapon))
		if nextAmmo then --and weaponslot > 0 then
			dxDrawText( "".. nextAmmo .."",textx, texty - 156, textx, texty - 156, tocolor(200,0,100,100), 1, "default", "center", "center", false,false,true,true)
		end
		
		
		dxDrawImage(left, top, 256, 128, "weap/".. currentWeapon ..".png", 0, 0, 0, tocolor(255, 255, 255, 255), true)
		currentAmmo = getPedTotalAmmo ( getLocalPlayer(), weaponslot ) - getPedAmmoInClip(getLocalPlayer(), weaponslot) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), weaponslot)
		if currentAmmo then --and weaponslot > 0 then
			dxDrawText( "".. currentAmmo .."",textx, texty + 48, textx, texty + 48, tocolor(200,0,100,255), 1.5, "default", "center", "center", false,false,true,true)
		end
		
		
		dxDrawImage(left + 26, top + currentHeight + gapBetween, 256-128, 128-64, "weap/".. previousWeapon ..".png", 0, 0, 0, tocolor(255, 255, 255, 100), true)
		previousAmmo = getPedTotalAmmo(getLocalPlayer(), getSlotFromWeapon(previousWeapon)) - getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(previousWeapon)) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(previousWeapon))
		if previousAmmo then --and weaponslot > 0 then
			dxDrawText( "".. previousAmmo .."",textx, texty + 156, textx, texty + 156, tocolor(200,0,100,100), 1, "default", "center", "center", false,false,true,true)
		end
		
		if kepszam == 0 then
			weaponslot = 0
		elseif kepszam == 1 and getPedWeapon(getLocalPlayer(), 1) > 0 then
			weaponslot = 1
		elseif kepszam == 2 and getPedWeapon(getLocalPlayer(), 2) > 0 then
			weaponslot = 2
		elseif kepszam == 3 and getPedWeapon(getLocalPlayer(), 3) > 0 then
			weaponslot = 3
		elseif kepszam == 4 and getPedWeapon(getLocalPlayer(), 4) > 0 then
			weaponslot = 4
		elseif kepszam == 5 and getPedWeapon(getLocalPlayer(), 5) > 0 then
			weaponslot = 5
		elseif kepszam == 6 and getPedWeapon(getLocalPlayer(), 6) > 0 then
			weaponslot = 6
		elseif kepszam == 7 and getPedWeapon(getLocalPlayer(), 7) > 0 then
			weaponslot = 7
		elseif kepszam == 8 and getPedWeapon(getLocalPlayer(), 8) > 0 then
			weaponslot = 8
		elseif kepszam == 9 and getPedWeapon(getLocalPlayer(), 9) > 0 then
			weaponslot = 9
		elseif kepszam == 10 and getPedWeapon(getLocalPlayer(), 10) > 0 then
			weaponslot = 10
		elseif kepszam == 11 and getPedWeapon(getLocalPlayer(), 11) > 0 then
			weaponslot = 11
		elseif kepszam == 12 and getPedWeapon(getLocalPlayer(), 12) > 0 then
			weaponslot = 12
		else
			weaponslot = 0
			nextAmmo = nil
			currentAmmo = nil
			previousAmmo = nil
		end
	end
end
addEventHandler("onClientRender", getRootElement(), selectWeapon)

addEventHandler( "onClientPedChoke", getRootElement( ),
    function ( )
        cancelEvent( );
    end
);

addEventHandler( "onClientResourceStart", getRootElement( ),
    function ( )
        toggleControl ( "next_weapon", false )
        toggleControl ( "previous_weapon", false )
    end
);

function playerPressingKey(key, state)
	if key == "mouse_wheel_up" and mutat then
		kepszam = kepszam + 1
		if kepszam > 12 then
			kepszam = 0
		end
	elseif key == "mouse_wheel_down" and mutat then
		kepszam = kepszam - 1
		if kepszam < 0 then
			kepszam = 12
		end
	elseif key == "q" and state then
		mutat = true
		for k, control in ipairs ( controls ) do
			toggleControl ( control, false )
		end
	elseif key == "q" then
		mutat = false
		for k, control in ipairs ( controls ) do
			toggleControl ( control, true )
		end
	elseif key == "mouse1" and state and mutat then
		for k, control in ipairs ( controls ) do
			toggleControl ( control, false )
		end
		setPedWeaponSlot ( getLocalPlayer(), weaponslot )
	end
end
addEventHandler("onClientKey", getRootElement(), playerPressingKey)

 

Link to comment
4 hours ago, Dimos7 said:

local screenWidth, screenHeight = guiGetScreenSize()

local currentWidth, currentHeight = 250, 125
local currentOutline = 4
local gapBetween = 32

local left = (screenWidth/2) - (currentWidth/2)
local top = (screenHeight/2) - (currentHeight/2)

local controls = { "aim_weapon","radio_next","radio_previous", "fire", "vehicle_fire", "vehicle_secondary_fire" }

local kepszam = 0
local weaponUpper = 0
local weaponLower = 0
local mutat = false

local weaponslot = 0

local nextAmmo = nil
local currentAmmo = nil
local previousAmmo = nil

function getPreviousWeapon() 
    local tempSlot = {} 
    local curSlot = getPedWeaponSlot(localPlayer) 
    if (curSlot == 0) and (getPedWeapon(localPlayer) == 0 or getPedWeapon(localPlayer) == 1) then 
        curSlot = 13 
    end 
     
    tempSlot["ID"] = 13 
    repeat 
        tempSlot["ID"] = tempSlot["ID"]-1 
    until (tempSlot["ID"] < curSlot) and not (tempSlot["ID"] ~= 0 and getPedWeapon(localPlayer, tempSlot["ID"]) == 0) 
    return getPedWeapon(localPlayer, tempSlot["ID"]) 
end

function getNextWeapon()
    local tempSlot = {} 
    local curSlot = getPedWeaponSlot(localPlayer) 
    if (curSlot == 12) then 
        curSlot = - 1 
    end 
     
    tempSlot["ID"] = - 1 
    repeat 
        tempSlot["ID"] = tempSlot["ID"] + 1 
        if tempSlot["ID"] == 13 then 
            tempSlot["ID"] = -1 
            curSlot = -1 
        end 
    until (tempSlot["ID"] > curSlot) and not (tempSlot["ID"] ~= 0 and getPedWeapon(localPlayer, tempSlot["ID"]) == 0) 
    return getPedWeapon(localPlayer, tempSlot["ID"]) 
end

function selectWeapon()
	if mutat then
		local previousWeapon = getPreviousWeapon()
		local currentWeapon = getPedWeapon(getLocalPlayer(), getPedWeaponSlot(getLocalPlayer()))
		local nextWeapon = getNextWeapon()
		
		local textx = left + (currentWidth / 2)
		local texty = (top + (currentHeight / 2))
		dxDrawText( "Fegyvereid",textx, texty - 215, textx, texty - 215, tocolor(200,0,100,255), 2, "default", "center", "center", false,false,true)
		
		if weaponslot == getPedWeaponSlot(getLocalPlayer()) then
			dxDrawRectangle ( left - currentOutline, top - currentOutline, currentWidth + currentOutline*2, currentHeight + currentOutline*2, tocolor ( 200, 0, 100, 180 ) )
		end
		dxDrawRectangle ( left, top, currentWidth, currentHeight, tocolor ( 0, 0, 0, 180 ) )
		
		dxDrawRectangle ( left + 26, top + 48 - currentHeight - gapBetween, currentWidth - 52, currentHeight - 32, tocolor ( 0, 0, 0, 180 ) )
		dxDrawRectangle ( left + 26, top - 16 + currentHeight + gapBetween, currentWidth - 52, currentHeight - 32, tocolor ( 0, 0, 0, 180 ) )
		
		
		dxDrawImage(left + 26, top - currentHeight + gapBetween, 256-128, 128-64, "weap/".. nextWeapon ..".png", 0, 0, 0, tocolor(255, 255, 255, 100), true)
		nextAmmo = getPedTotalAmmo(getLocalPlayer(), getSlotFromWeapon(nextWeapon)) - getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(nextWeapon)) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(nextWeapon))
		if nextAmmo then --and weaponslot > 0 then
			dxDrawText( "".. nextAmmo .."",textx, texty - 156, textx, texty - 156, tocolor(200,0,100,100), 1, "default", "center", "center", false,false,true,true)
		end
		
		
		dxDrawImage(left, top, 256, 128, "weap/".. currentWeapon ..".png", 0, 0, 0, tocolor(255, 255, 255, 255), true)
		currentAmmo = getPedTotalAmmo ( getLocalPlayer(), weaponslot ) - getPedAmmoInClip(getLocalPlayer(), weaponslot) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), weaponslot)
		if currentAmmo then --and weaponslot > 0 then
			dxDrawText( "".. currentAmmo .."",textx, texty + 48, textx, texty + 48, tocolor(200,0,100,255), 1.5, "default", "center", "center", false,false,true,true)
		end
		
		
		dxDrawImage(left + 26, top + currentHeight + gapBetween, 256-128, 128-64, "weap/".. previousWeapon ..".png", 0, 0, 0, tocolor(255, 255, 255, 100), true)
		previousAmmo = getPedTotalAmmo(getLocalPlayer(), getSlotFromWeapon(previousWeapon)) - getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(previousWeapon)) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(previousWeapon))
		if previousAmmo then --and weaponslot > 0 then
			dxDrawText( "".. previousAmmo .."",textx, texty + 156, textx, texty + 156, tocolor(200,0,100,100), 1, "default", "center", "center", false,false,true,true)
		end
		
		if kepszam == 0 then
			weaponslot = 0
		elseif kepszam == 1 and getPedWeapon(getLocalPlayer(), 1) > 0 then
			weaponslot = 1
		elseif kepszam == 2 and getPedWeapon(getLocalPlayer(), 2) > 0 then
			weaponslot = 2
		elseif kepszam == 3 and getPedWeapon(getLocalPlayer(), 3) > 0 then
			weaponslot = 3
		elseif kepszam == 4 and getPedWeapon(getLocalPlayer(), 4) > 0 then
			weaponslot = 4
		elseif kepszam == 5 and getPedWeapon(getLocalPlayer(), 5) > 0 then
			weaponslot = 5
		elseif kepszam == 6 and getPedWeapon(getLocalPlayer(), 6) > 0 then
			weaponslot = 6
		elseif kepszam == 7 and getPedWeapon(getLocalPlayer(), 7) > 0 then
			weaponslot = 7
		elseif kepszam == 8 and getPedWeapon(getLocalPlayer(), 8) > 0 then
			weaponslot = 8
		elseif kepszam == 9 and getPedWeapon(getLocalPlayer(), 9) > 0 then
			weaponslot = 9
		elseif kepszam == 10 and getPedWeapon(getLocalPlayer(), 10) > 0 then
			weaponslot = 10
		elseif kepszam == 11 and getPedWeapon(getLocalPlayer(), 11) > 0 then
			weaponslot = 11
		elseif kepszam == 12 and getPedWeapon(getLocalPlayer(), 12) > 0 then
			weaponslot = 12
		else
			weaponslot = 0
			nextAmmo = nil
			currentAmmo = nil
			previousAmmo = nil
		end
	end
end
addEventHandler("onClientRender", getRootElement(), selectWeapon)

addEventHandler( "onClientPedChoke", getRootElement( ),
    function ( )
        cancelEvent( );
    end
);

addEventHandler( "onClientResourceStart", getRootElement( ),
    function ( )
        toggleControl ( "next_weapon", false )
        toggleControl ( "previous_weapon", false )
    end
);

function playerPressingKey(key, state)
	if key == "mouse_wheel_up" and mutat then
		kepszam = kepszam + 1
		if kepszam > 12 then
			kepszam = 0
		end
	elseif key == "mouse_wheel_down" and mutat then
		kepszam = kepszam - 1
		if kepszam < 0 then
			kepszam = 12
		end
	elseif key == "q" and state then
		mutat = true
		for k, control in ipairs ( controls ) do
			toggleControl ( control, false )
		end
	elseif key == "q" then
		mutat = false
		for k, control in ipairs ( controls ) do
			toggleControl ( control, true )
		end
	elseif key == "mouse1" and state and mutat then
		for k, control in ipairs ( controls ) do
			toggleControl ( control, false )
		end
		setPedWeaponSlot ( getLocalPlayer(), weaponslot )
	end
end
addEventHandler("onClientKey", getRootElement(), playerPressingKey)

 

You didn't change anything.

Link to comment

I don't really have time to look at the actual code side, I'm currently lost in the .NET world, however, I made a little diagram about what you have to do. You already have the nextWeapon and previousWeapon functions, that's good but you'll have to tweak it so you can actually specify the current weapon and get the next and previous weapon relative to that.

Look at this diagram, I always make diagrams like this, helps you visualize the problem: 

 

ce06c5b1a1ce42ed8fee4bed2b9dc5e7.png

 

Hope it helps.

Link to comment
8 minutes ago, pa3ck said:

I don't really have time to look at the actual code side, I'm currently lost in the .NET world, however, I made a little diagram about what you have to do. You already have the nextWeapon and previousWeapon functions, that's good but you'll have to tweak it so you can actually specify the current weapon and get the next and previous weapon relative to that.

Look at this diagram, I always make diagrams like this, helps you visualize the problem: 

 

ce06c5b1a1ce42ed8fee4bed2b9dc5e7.png

 

Hope it helps.

Thank you for taking time on creating this diagram and replying, I will try it tomorrow, right now I don't really understand, maybe it's because I don't see the script and I'm tired, but thank you, will try something again!

Link to comment

I changed a few settings a little bit, because this is not how I want it to work, but I am also getting some errors and it's not working properly. I changed it back so if I hold Q or a specified button, that is in this case is the Q, then the selector will appear. I could fix a bug where you was only able to switch between melee weapons and fist if you scrolled up and the bug that I got errors about wrong weapon slots. They were strings and I was getting errors about that strings, but I changed them to 0 in the table and now it works. But I don't know one thing, what to change to the weaps["curr"] variable at the changeWeap() function? If I change it to 0 it not works properly, neither if I change it to anything else. Do you have any idea what to change to this one? Or how to fix this problem? (As I said, I have edited the script to work a little bit different, I want to force the players to hold the button if they want to change weapons.)

local screenWidth, screenHeight = guiGetScreenSize()
 
local currentWidth, currentHeight = 250, 125
local currentOutline = 4
local gapBetween = 32
 
local left = (screenWidth/2) - (currentWidth/2)
local top = (screenHeight/2) - (currentHeight/2)
 
local controls = { "aim_weapon","radio_next","radio_previous", "fire", "vehicle_fire", "vehicle_secondary_fire" }

local isRendered
 
local nextAmmo = nil
local currentAmmo = nil
local previousAmmo = nil
 
function getNextWeapon(cSlot)
    local tempSlot = {}
    local curSlot
    if cSlot then
        curSlot = cSlot
    else
        curSlot = getPedWeaponSlot(localPlayer)
    end
    if (curSlot == 0) then
        curSlot = 13
    end
     
    tempSlot["ID"] = 13
    repeat
        tempSlot["ID"] = tempSlot["ID"]-1
    until (tempSlot["ID"] < curSlot) and not (tempSlot["ID"] ~= 0 and getPedWeapon(localPlayer, tempSlot["ID"]) == 0)
    return getPedWeapon(localPlayer, tempSlot["ID"])
end
 
function getPreviousWeapon(cSlot)
    local tempSlot = {}
    local curSlot
    if cSlot then
        curSlot = cSlot
    else
        curSlot = getPedWeaponSlot(localPlayer)
    end
    if (curSlot == 13) then
        curSlot = 0
    end
     
    tempSlot["ID"] = - 1
    repeat
        tempSlot["ID"] = tempSlot["ID"] + 1
        if tempSlot["ID"] == 13 then
            tempSlot["ID"] = -1
            curSlot = -1
        end
    until (tempSlot["ID"] > curSlot) and not (tempSlot["ID"] ~= 0 and getPedWeapon(localPlayer, tempSlot["ID"]) == 0)
    return getPedWeapon(localPlayer, tempSlot["ID"])
end
 
local weaps = {
    ["old"] = 0,
    ["curr"] = 0,
    ["next"] = 0,
    ["prev"] = 0
}
 
function changeNextWeapon()
    if weaps["curr"] == 0 then
        weaps["old"] = getPedWeapon(getLocalPlayer())
        weaps["curr"] = weaps["old"]
    else
        weaps["curr"] = getNextWeapon(getSlotFromWeapon(weaps["curr"]))
    end
end

function changePrevWeapon()
    if weaps["curr"] == 0 then
        weaps["old"] = getPedWeapon(getLocalPlayer())
        weaps["curr"] = weaps["old"]
    else
        weaps["curr"] = getPreviousWeapon(getSlotFromWeapon(weaps["curr"]))
    end
end
 
function changeWeap()
    setPedWeaponSlot(localPlayer, getSlotFromWeapon(weaps["curr"]))
    weaps["curr"] = weaps["prev"]
end
 
function selectWeapon()
        weaps["next"] = getNextWeapon(getSlotFromWeapon(weaps["curr"]))
        weaps["prev"] = getPreviousWeapon(getSlotFromWeapon(weaps["curr"]))
       
        local textx = left + (currentWidth / 2)
        local texty = (top + (currentHeight / 2))
        dxDrawText( "Fegyvereid",textx, texty - 215, textx, texty - 215, tocolor(200,0,100,255), 2, "default", "center", "center", false,false,true)
       
        if weaps["old"] == weaps["curr"] then
            dxDrawRectangle ( left - currentOutline, top - currentOutline, currentWidth + currentOutline*2, currentHeight + currentOutline*2, tocolor ( 200, 0, 100, 180 ) )
        end
        dxDrawRectangle ( left, top, currentWidth, currentHeight, tocolor ( 0, 0, 0, 180 ) )
       
        dxDrawRectangle ( left + 26, top + 48 - currentHeight - gapBetween, currentWidth - 52, currentHeight - 32, tocolor ( 0, 0, 0, 180 ) )
        dxDrawRectangle ( left + 26, top - 16 + currentHeight + gapBetween, currentWidth - 52, currentHeight - 32, tocolor ( 0, 0, 0, 180 ) )
       
       
        dxDrawImage(left + 26, top - currentHeight + gapBetween, 256-128, 128-64, "weap/".. weaps["next"] ..".png", 0, 0, 0, tocolor(255, 255, 255, 100), true)
        nextAmmo = getPedTotalAmmo(getLocalPlayer(), getSlotFromWeapon(weaps["next"])) - getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(weaps["next"])) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(weaps["next"]))
        if nextAmmo then --and weaponslot > 0 then
            dxDrawText( "".. nextAmmo .."",textx, texty - 156, textx, texty - 156, tocolor(200,0,100,100), 1, "default", "center", "center", false,false,true,true)
        end
       
       
        dxDrawImage(left, top, 256, 128, "weap/".. weaps["curr"] ..".png", 0, 0, 0, tocolor(255, 255, 255, 255), true)
        currentAmmo = getPedTotalAmmo ( getLocalPlayer(), getSlotFromWeapon(weaps["curr"])) - getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(weaps["curr"])) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(weaps["curr"]))
        if currentAmmo then --and weaponslot > 0 then
            dxDrawText( "".. currentAmmo .."",textx, texty + 48, textx, texty + 48, tocolor(200,0,100,255), 1.5, "default", "center", "center", false,false,true,true)
        end
       
       
        dxDrawImage(left + 26, top + currentHeight + gapBetween, 256-128, 128-64, "weap/".. weaps["prev"] ..".png", 0, 0, 0, tocolor(255, 255, 255, 100), true)
        previousAmmo = getPedTotalAmmo(getLocalPlayer(), getSlotFromWeapon(weaps["prev"])) - getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(weaps["prev"])) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), getSlotFromWeapon(weaps["prev"]))
        if previousAmmo then --and weaponslot > 0 then
            dxDrawText( "".. previousAmmo .."",textx, texty + 156, textx, texty + 156, tocolor(200,0,100,100), 1, "default", "center", "center", false,false,true,true)
        end
end
 
addEventHandler( "onClientResourceStart", getRootElement( ),
    function ( )
        toggleControl ( "next_weapon", false )
        toggleControl ( "previous_weapon", false )
    end
);

function onClientKey(key, state)
	if key == "mouse_wheel_up" and isRendered then
		changeNextWeapon()
	elseif key == "mouse_wheel_down" and isRendered then
		changePrevWeapon()
	elseif key == "q" and state and not isRendered then
		addEventHandler("onClientRender", root, selectWeapon)
		isRendered = not isRendered
		for k, control in ipairs ( controls ) do
			toggleControl ( control, false )
		end
	elseif key == "q" then
		removeEventHandler("onClientRender", root, selectWeapon)
		isRendered = not isRendered
		for k, control in ipairs ( controls ) do
			toggleControl ( control, true )
		end
	elseif key == "mouse1" and state and isRendered then
		changeWeap()
		for k, control in ipairs ( controls ) do
			toggleControl ( control, false )
		end
	end
end
addEventHandler("onClientKey", getRootElement(), onClientKey)

 

Link to comment

It did work for me actually, I was only getting warnings that it might not work in the future version because of the negative value. Anyway, because you changed the code and you draw the render when the Q is pressed and not the way I did it (when you mouse wheel up-down) you have to define the current weapon. Put this snippet in the Q function, where you start drawing the render (where you have the addEventHandler.onClientRender..)

 

if weaps["curr"] == 0 then
	weaps["old"] = getPedWeapon(localPlayer)
	weaps["curr"] = weaps["old"]
end

 

Link to comment

I managed to fix it, the "handling" of it works perfect now, only problems are that I get errors in debugscript about getSlotFromWeapon. It says the following: Expected weapon-type at argument 1, got string, and it's at line 259 and 260 which are these lines:

weaps["next"] = tonumber(getNextWeapon(getSlotFromWeapon(weaps["curr"])))
weaps["prev"] = tonumber(getPreviousWeapon(getSlotFromWeapon(weaps["curr"])))

It's inside the selectWeapon function, the function that draws the selector.

function selectWeapon()
        weaps["next"] = tonumber(getNextWeapon(getSlotFromWeapon(weaps["curr"])))
        weaps["prev"] = tonumber(getPreviousWeapon(getSlotFromWeapon(weaps["curr"])))
       
		if weaps["curr"] == "" then
			weaps["old"] = getPedWeapon(localPlayer)
			weaps["curr"] = weaps["old"]
		end
	   
        local textx = left + (currentWidth / 2)
        local texty = (top + (currentHeight / 2))
        dxDrawText( "Fegyvereid",textx, texty - 215, textx, texty - 215, tocolor(255,255,255,255), 2, "default", "center", "center", false,false,true)
       
        if weaps["curr"] == weaps["old"] then
            dxDrawRectangle ( left - currentOutline, top - currentOutline, currentWidth + currentOutline*2, currentHeight + currentOutline*2, tocolor ( 200, 0, 100, 180 ) )
        elseif weaps["prev"] == weaps["old"] then
			dxDrawRectangle ( left + 26 - currentOutline, top - 16 + currentHeight + gapBetween - currentOutline, currentWidth - 52 + currentOutline*2, currentHeight - 32 + currentOutline*2, tocolor ( 200, 0, 100, 75 ) )
		elseif weaps["next"] == weaps["old"] then
			dxDrawRectangle ( left + 26 - currentOutline, top + 48 - currentHeight - gapBetween - currentOutline, currentWidth - 52 + currentOutline*2, currentHeight - 32 + currentOutline*2, tocolor ( 200, 0, 100, 75 ) )
		end
		
        dxDrawRectangle ( left, top, currentWidth, currentHeight, tocolor ( 0, 0, 0, 180 ) )
       
        dxDrawRectangle ( left + 26, top + 48 - currentHeight - gapBetween, currentWidth - 52, currentHeight - 32, tocolor ( 0, 0, 0, 75 ) )
        dxDrawRectangle ( left + 26, top - 16 + currentHeight + gapBetween, currentWidth - 52, currentHeight - 32, tocolor ( 0, 0, 0, 75 ) )
       
       
        dxDrawImage(left + 26, top - currentHeight + gapBetween, 256-128, 128-64, "weap/".. weaps["next"] ..".png", 0, 0, 0, tocolor(255, 255, 255, 100), true)
        nextAmmo = getPedTotalAmmo(getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["next"]))) - getPedAmmoInClip(getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["next"]))) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["next"])))
        if nextAmmo and getSlotFromWeapon(weaps["next"]) ~= 0 and getSlotFromWeapon(weaps["next"]) ~= 1 and getSlotFromWeapon(weaps["next"]) ~= 10 then
            dxDrawText( "".. tostring(nextAmmo) .."",textx, texty - 156, textx, texty - 156, tocolor(200,0,100,100), 1, "default", "center", "center", false,false,true,true)
        end
       
        dxDrawImage(left, top, 256, 128, "weap/".. weaps["curr"] ..".png", 0, 0, 0, tocolor(255, 255, 255, 255), true)
        currentAmmo = getPedTotalAmmo ( getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["curr"]))) - getPedAmmoInClip(getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["curr"]))) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["curr"])))
		if currentAmmo and getSlotFromWeapon(weaps["curr"]) ~= 0 and getSlotFromWeapon(weaps["curr"]) ~= 1 and getSlotFromWeapon(weaps["curr"]) ~= 10 then
            dxDrawText( "".. tostring(currentAmmo) .."",textx, texty + 48, textx, texty + 48, tocolor(200,0,100,255), 1.5, "default", "center", "center", false,false,true,true)
        end
       
        dxDrawImage(left + 26, top + currentHeight + gapBetween, 256-128, 128-64, "weap/".. weaps["prev"] ..".png", 0, 0, 0, tocolor(255, 255, 255, 100), true)
        previousAmmo = getPedTotalAmmo(getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["prev"]))) - getPedAmmoInClip(getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["prev"]))) .. " #FFFFFF/ " .. getPedAmmoInClip(getLocalPlayer(), tonumber(getSlotFromWeapon(weaps["prev"])))
        if previousAmmo and getSlotFromWeapon(weaps["prev"]) ~= 0 and getSlotFromWeapon(weaps["prev"]) ~= 1 and getSlotFromWeapon(weaps["prev"]) ~= 10 then
            dxDrawText( "".. tostring(previousAmmo) .."",textx, texty + 156, textx, texty + 156, tocolor(200,0,100,100), 1, "default", "center", "center", false,false,true,true)
        end
end

Is there any way to fix this error spamming? It only happens when I change to a new weapon (press left click).

 

The other problem is when I give myself a new weapon from for example admin panel, the picture of the current weapon and the pink outline doesn't get changed. So lets say I have an AK-47. I give myself an MP5 and when I press Q, at the current weapon I still see the AK-47 and that it's selected, and not the MP5. How can I fix that as well? So when I get a new weapon, the picture and weaponslot data gets updated or something like that. Hope you know what I mean, and thank you for helping!

Link to comment

I don't know what changes you made to the code, but probably the weaps["curr"] is empty when you are trying to get the next and prev weapon. I already told you what to do, initialize the "curr" to the current BEFORE drawing anything. You don't need to use getWeaponIDFromName, you never even touch the weapon names in the code, the reason why it's string is because that's the default value in the table. 

For your other problem change this:

if weaps["curr"] == weaps["old"] then 

to this:

if weaps["curr"] == getPedWeapon(localPlayer) then

so when you give yourself a weapon, the highlight will change accordingly.

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...