Jump to content

[HELP] Sniper Crosshairs


India

Recommended Posts

Hello, i have a script with sniper crosshair but there is a problem

Example: DMR weapon have 1 bullet, when its reloading, if i don't release the right mouse button crosshair not closing. i want it to disable its crosshair while reloading ammo. Anyone can help me?

client.lua

local textures = {}; 
local cross = false

local function render_crosshair()
	local screenw,screenh = guiGetScreenSize (); 
	local s = screenw * 0.5; 
	local u,v = (screenw - s)*0.5, (screenh - s)*0.5;
	local black = tocolor (0, 0, 0, 255);
	dxDrawRectangle (0, 0, screenw, v, black);
	dxDrawRectangle (0, 0, u, screenh, black);
	dxDrawRectangle (0, screenh, screenw, -v, black);
	dxDrawRectangle (screenw, screenh, -u, -screenh, black);
	dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["crosshair_background"]);
	if cross == "DMR" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_dmr"]);
	elseif cross == "AS50" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_as50"]);
	elseif cross == "VSS" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_vss"]);
	elseif cross == "VSSK" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_vss"]);
	elseif cross == "M107" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_as50"]);	
	elseif cross == "M2000 CheyTac" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_as50"]);			
	end
end;


local function on_weapon_aimed (key, state)
	local weapon_sniper_rifle = 34; 
	if getPedWeapon (getLocalPlayer ()) == weapon_sniper_rifle then 
		if state == "down" then 
		cross = getElementData(getLocalPlayer(),"currentweapon_1")
			setPlayerHudComponentVisible ("crosshair", false);
			addEventHandler ("onClientHUDRender", getRootElement (), render_crosshair);
		else 
		cross = false
			removeEventHandler ("onClientHUDRender", getRootElement (), render_crosshair); 
		end;
	end;
end; 

addEventHandler ("onClientResourceStart", getResourceRootElement (getThisResource ()),
	function () 
	textures ["crosshair_background"] = dxCreateTexture ("images/crosshairs/crossBackground1.png", "argb", true, "wrap")
	textures ["cross_dmr"] = dxCreateTexture ("images/crosshairs/aim_dmr.png", "argb", true, "wrap"); 
	textures ["cross_as50"] = dxCreateTexture ("images/crosshairs/aim_as50.png", "argb", true, "wrap"); 
	textures ["cross_vss"] = dxCreateTexture ("images/crosshairs/aim_vss.png", "argb", true, "wrap"); 

		bindKey ("aim_weapon", "both", on_weapon_aimed);
		addEventHandler ("onClientPlayerWeaponSwitch", getRootElement (),
			function (previousWeaponSlot)
				local weaponslot_type_sniper = 6; 
				if previousWeaponSlot == weaponslot_type_sniper then 
					if not isPlayerHudComponentVisible ("crosshair") then 
						setPlayerHudComponentVisible ("crosshair", true);
					end;
				end; 
			end);
	end); 
	
addEventHandler ("onClientResourceStop", getResourceRootElement (getThisResource ()),
	function ()
		unbindKey ("aim_weapon", "both", on_weapon_aimed); 
		if not isPlayerHudComponentVisible ("crosshair") then
			setPlayerHudComponentVisible ("crosshair", true);
		end;
	end);

Pictures for details;

Spoiler

1-1.png

Spoiler

1-2.png

Spoiler

1-3.png

 

Link to comment
On 14/02/2021 at 18:29, India said:

Hello, i have a script with sniper crosshair but there is a problem

Example: DMR weapon have 1 bullet, when its reloading, if i don't release the right mouse button crosshair not closing. i want it to disable its crosshair while reloading ammo. Anyone can help me?

client.lua


local textures = {}; 
local cross = false

local function render_crosshair()
	local screenw,screenh = guiGetScreenSize (); 
	local s = screenw * 0.5; 
	local u,v = (screenw - s)*0.5, (screenh - s)*0.5;
	local black = tocolor (0, 0, 0, 255);
	dxDrawRectangle (0, 0, screenw, v, black);
	dxDrawRectangle (0, 0, u, screenh, black);
	dxDrawRectangle (0, screenh, screenw, -v, black);
	dxDrawRectangle (screenw, screenh, -u, -screenh, black);
	dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["crosshair_background"]);
	if cross == "DMR" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_dmr"]);
	elseif cross == "AS50" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_as50"]);
	elseif cross == "VSS" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_vss"]);
	elseif cross == "VSSK" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_vss"]);
	elseif cross == "M107" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_as50"]);	
	elseif cross == "M2000 CheyTac" then
	    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["cross_as50"]);			
	end
end;


local function on_weapon_aimed (key, state)
	local weapon_sniper_rifle = 34; 
	if getPedWeapon (getLocalPlayer ()) == weapon_sniper_rifle then 
		if state == "down" then 
		cross = getElementData(getLocalPlayer(),"currentweapon_1")
			setPlayerHudComponentVisible ("crosshair", false);
			addEventHandler ("onClientHUDRender", getRootElement (), render_crosshair);
		else 
		cross = false
			removeEventHandler ("onClientHUDRender", getRootElement (), render_crosshair); 
		end;
	end;
end; 

addEventHandler ("onClientResourceStart", getResourceRootElement (getThisResource ()),
	function () 
	textures ["crosshair_background"] = dxCreateTexture ("images/crosshairs/crossBackground1.png", "argb", true, "wrap")
	textures ["cross_dmr"] = dxCreateTexture ("images/crosshairs/aim_dmr.png", "argb", true, "wrap"); 
	textures ["cross_as50"] = dxCreateTexture ("images/crosshairs/aim_as50.png", "argb", true, "wrap"); 
	textures ["cross_vss"] = dxCreateTexture ("images/crosshairs/aim_vss.png", "argb", true, "wrap"); 

		bindKey ("aim_weapon", "both", on_weapon_aimed);
		addEventHandler ("onClientPlayerWeaponSwitch", getRootElement (),
			function (previousWeaponSlot)
				local weaponslot_type_sniper = 6; 
				if previousWeaponSlot == weaponslot_type_sniper then 
					if not isPlayerHudComponentVisible ("crosshair") then 
						setPlayerHudComponentVisible ("crosshair", true);
					end;
				end; 
			end);
	end); 
	
addEventHandler ("onClientResourceStop", getResourceRootElement (getThisResource ()),
	function ()
		unbindKey ("aim_weapon", "both", on_weapon_aimed); 
		if not isPlayerHudComponentVisible ("crosshair") then
			setPlayerHudComponentVisible ("crosshair", true);
		end;
	end);

Pictures for details;

  Hide contents

1-1.png

  Hide contents

1-2.png

  Hide contents

1-3.png

 

For this you may use this little script

--server side

function reloadWeapon(weapon, ammo)
    if client then 
        takeWeapon( client, weapon )
        giveWeapon( client, weapon, ammo, true)
    end
end
addEvent("relWep", true)
addEventHandler("relWep", resourceRoot, reloadWeapon)
--client side
local slots = {
    [5] = {
        aimDelay = 1000,
    }, 
    [6] = {
        aimDelay = 1000,
    }
}

local canAim = true

function hideRadar(player, slot, weapon, ammo, aimDelay)
    if slot == 0 then return end
    setPedWeaponSlot(player,0)
    canAim = false
    if ammo > 1 then 
        if (not isPedDucked(player)) then 
            setPedAnimation(player, "silenced", "silence_reload", aimDelay, false, false, false, false)
        end
        triggerServerEvent("relWep", resourceRoot, weapon, ammo)
    end
    setTimer(function() canAim = true end, aimDelay, 1)
end

function playerPressedKey(button, press)
    if (press) and button == "mouse2" then
        if canAim == false then cancelEvent() end
    end
end
addEventHandler("onClientKey", root, playerPressedKey)

addEventHandler("onClientPlayerWeaponFire", root, function() 

    local player = getLocalPlayer()
    local slot = getPedWeaponSlot(player)
    local weapon = getPedWeapon(player)
    local ammo = getPedTotalAmmo(player, slot)
    
    if (slots[slot]) then
        hideRadar(player, slot, weapon, ammo, slots[slot].aimDelay)
    end

end)

 

Link to comment
5 hours ago, Ayush Rathore said:

For this you may use this little script


--server side

function reloadWeapon(weapon, ammo)
    if client then 
        takeWeapon( client, weapon )
        giveWeapon( client, weapon, ammo, true)
    end
end
addEvent("relWep", true)
addEventHandler("relWep", resourceRoot, reloadWeapon)

--client side
local slots = {
    [5] = {
        aimDelay = 1000,
    }, 
    [6] = {
        aimDelay = 1000,
    }
}

local canAim = true

function hideRadar(player, slot, weapon, ammo, aimDelay)
    if slot == 0 then return end
    setPedWeaponSlot(player,0)
    canAim = false
    if ammo > 1 then 
        if (not isPedDucked(player)) then 
            setPedAnimation(player, "silenced", "silence_reload", aimDelay, false, false, false, false)
        end
        triggerServerEvent("relWep", resourceRoot, weapon, ammo)
    end
    setTimer(function() canAim = true end, aimDelay, 1)
end

function playerPressedKey(button, press)
    if (press) and button == "mouse2" then
        if canAim == false then cancelEvent() end
    end
end
addEventHandler("onClientKey", root, playerPressedKey)

addEventHandler("onClientPlayerWeaponFire", root, function() 

    local player = getLocalPlayer()
    local slot = getPedWeaponSlot(player)
    local weapon = getPedWeapon(player)
    local ammo = getPedTotalAmmo(player, slot)
    
    if (slots[slot]) then
        hideRadar(player, slot, weapon, ammo, slots[slot].aimDelay)
    end

end)

 

I didn't mention it, i mean when sniper reloading, crosshair still showing or when i press "R" crosshair still showing 

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