Jump to content

[BUG]Lag in my custom weapons script


Soapbosnia

Recommended Posts

Hello guys,i made something which replaces explosion sounds,weapon sounds,weapon models,sniper scope,rocket crosshair  and disables default weapon sounds in 1 script,now the problem is whenever i shoot with my gun it makes me lag,i dont use any custom effects or stuff.Pls help.

Thank You.Btw heres the code if u need

local distance = 75 --distance from where you can hear the shot
local explostionDistance = 150

local cSoundsEnabled = true
local reloadSoundEnabled = false
local explosionEnabled = true
----RIP
function replaceWeapon()
 
txd = engineLoadTXD ( "Weapons/knife.txd" )
engineImportTXD ( txd, 335)
dff = engineLoadDFF ( "Weapons/knife.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 335) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/pistol.txd" )
engineImportTXD ( txd, 346)
dff = engineLoadDFF ( "Weapons/pistol.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 346) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/silenced.txd" )
engineImportTXD ( txd, 347)
dff = engineLoadDFF ( "Weapons/silenced.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 347) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/deagle.txd" )
engineImportTXD ( txd, 348)
dff = engineLoadDFF ( "Weapons/deagle.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 348) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/shotgun.txd" )
engineImportTXD ( txd, 349)
dff = engineLoadDFF ( "Weapons/shotgun.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 349) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/sawnoff.txd" )
engineImportTXD ( txd, 350)
dff = engineLoadDFF ( "Weapons/sawnoff.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 350) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/striker.txd" )
engineImportTXD ( txd, 351)
dff = engineLoadDFF ( "Weapons/striker.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 351) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/uzi.txd" )
engineImportTXD ( txd, 352)
dff = engineLoadDFF ( "Weapons/uzi.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 352) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/mp5.txd" )
engineImportTXD ( txd, 353)
dff = engineLoadDFF ( "Weapons/mp5.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 353) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/tec9.txd" )
engineImportTXD ( txd, 372)
dff = engineLoadDFF ( "Weapons/tec9.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 372) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/ak47.txd" )
engineImportTXD ( txd, 355)
dff = engineLoadDFF ( "Weapons/ak47.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 355) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/m4.txd" )
engineImportTXD ( txd, 356)
dff = engineLoadDFF ( "Weapons/m4.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 356) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/rifle.txd" )
engineImportTXD ( txd, 357)
dff = engineLoadDFF ( "Weapons/rifle.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 357) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/sniper.txd" )
engineImportTXD ( txd, 358)
dff = engineLoadDFF ( "Weapons/sniper.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 358) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/launcher.txd" )
engineImportTXD ( txd, 359)
dff = engineLoadDFF ( "Weapons/launcher.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 359) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/hlauncher.txd" )
engineImportTXD ( txd, 360)
dff = engineLoadDFF ( "Weapons/hlauncher.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 360) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/minigun.txd" )
engineImportTXD ( txd, 362)
dff = engineLoadDFF ( "Weapons/minigun.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 362) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/grenade.txd" )
engineImportTXD ( txd, 342)
dff = engineLoadDFF ( "Weapons/grenade.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 342) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/teargas.txd" )
engineImportTXD ( txd, 343)
dff = engineLoadDFF ( "Weapons/teargas.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 343) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/molotov.txd" )
engineImportTXD ( txd, 344)
dff = engineLoadDFF ( "Weapons/molotov.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 344) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/satchel.txd" )
engineImportTXD ( txd, 363)
dff = engineLoadDFF ( "Weapons/satchel.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 363) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/parachute.txd" )
engineImportTXD ( txd, 371)
dff = engineLoadDFF ( "Weapons/parachute.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 371) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----RIP
function replaceWeapon() 
txd = engineLoadTXD ( "Weapons/bomb.txd" )
engineImportTXD ( txd, 364)
dff = engineLoadDFF ( "Weapons/bomb.dff") -- use weapon model ID, not GTA weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
engineReplaceModel ( dff, 364) -- Likewise, model ID, for M4 as example it's 356

end
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
----Adds New Explosion Sounds
addEventHandler("onClientExplosion", getRootElement(), function(x,y,z, theType) 
    local Sdimension = getElementDimension(source) 
    local Sinterior = getElementInterior(source) 
    if(explosionEnabled)then 
        if(theType == 0)then--Grenade 
            local explSound = playSound3D("Explosions/explosion1.mp3", x,y,z) 
            setSoundMaxDistance(explSound, explostionDistance) 
            setElementDimension(explSound, Sdimension) 
            setElementInterior(explSound, Sinterior) 
        elseif(theType == 4 or theType == 5 or theType == 6 or theType == 7)then --car, car quick, boat, heli 
            local explSound = playSound3D("Explosions/explosion3.mp3", x,y,z) 
            setSoundMaxDistance(explSound, explostionDistance) 
            setElementDimension(explSound, Sdimension) 
            setElementInterior(explSound, Sinterior) 
        elseif(theType == 1)then 
            else 
            local explSound = playSound3D("Explosions/explosion2.mp3", x,y,z) 
            setSoundMaxDistance(explSound, explostionDistance) 
            setElementDimension(explSound, Sdimension) 
            setElementInterior(explSound, Sinterior) 
		end
	end
end)
----Adds New Weapon Sounds
function playSounds(weapon, ammo, ammoInClip)
	if(cSoundsEnabled)then
		local x,y,z = getElementPosition(source)
		if weapon == 31 then --m4
			if(ammoInClip == 0 and reloadSoundEnabled)then
				mgReload("Sounds/m4.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/m4.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
		elseif weapon == 22 then --pistol
			if(ammoInClip == 0 and reloadSoundEnabled)then
				pistolReload("Sounds/pistole.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/pistole.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
		elseif weapon == 23 then --silenced
			if(ammoInClip == 0 and reloadSoundEnabled)then
				pistolReload("Sounds/silenced.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/silenced.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end	
		elseif weapon == 24 then --deagle
			if(ammoInClip == 0 and reloadSoundEnabled)then
				pistolReload("Sounds/deagle.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/deagle.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
		elseif weapon == 25 then --shotgun
			if(weapon == 25)then
				local sound = playSound3D("Sounds/shotgun.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
				shotgunReload(x,y,z)
			else
				local sound = playSound3D("Sounds/shotgun.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
		elseif weapon == 28 then --uzi
			if(ammoInClip == 0)then						
				mgReload("Sounds/uzi.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/uzi.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
			elseif weapon == 38 then --minigun
			if(ammoInClip == 0)then						
				mgReload("Sounds/minigun.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/minigun.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
		elseif weapon == 29 then --mp5
			if(ammoInClip == 0 and reloadSoundEnabled)then
				mgReload("Sounds/mp5.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/mp5.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
		elseif weapon == 26 then --sawnoff
			if(ammoInClip == 0 and reloadSoundEnabled)then
				mgReload("Sounds/sawnoff.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/sawnoff.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end	
		elseif weapon == 27 then --sawnoff
			if(ammoInClip == 0 and reloadSoundEnabled)then
				mgReload("Sounds/striker.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/striker.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end		
		elseif weapon == 32 then --tec-9
			if(ammoInClip == 0)then						
				tec9Reload(x,y,z)
			else
				local sound = playSound3D("Sounds/tec-9.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
		elseif weapon == 30 then --ak
			if(ammoInClip == 0 and reloadSoundEnabled)then
				mgReload("Sounds/ak.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/ak.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end
		elseif weapon == 33 then --rifle
			if(ammoInClip == 0 and reloadSoundEnabled)then
				mgReload("Sounds/rifle.wav", x,y,z)
			else
				local sound = playSound3D("Sounds/rifle.wav", x,y,z)
				setSoundMaxDistance(sound, distance)
			end	
		elseif  weapon == 34 then --sniper
			local sound = playSound3D("sounds/sniper.wav", x,y,z)
			setSoundMaxDistance(sound, distance)
		end
	end
end
addEventHandler("onClientPlayerWeaponFire", getRootElement(), playSounds)
----Disables Default Explosion And Weapon Sounds
setWorldSoundEnabled(5, false) 
function removeWorldSounds() 
setWorldSoundEnabled(5, 3, false) 
setWorldSoundEnabled(5, 4, false) 
setWorldSoundEnabled(5, 5, false) 
setWorldSoundEnabled(5, 6, false) 
setWorldSoundEnabled(5, 7, false) 
setWorldSoundEnabled(5, 8, false) 
setWorldSoundEnabled(5, 11, false) 
setWorldSoundEnabled(5, 12, false) 
setWorldSoundEnabled(5, 13, false) 
setWorldSoundEnabled(5, 14, false) 
setWorldSoundEnabled(5, 15, false) 
setWorldSoundEnabled(5, 16, false) 
setWorldSoundEnabled(5, 17, false) 
setWorldSoundEnabled(5, 18, false) 
setWorldSoundEnabled(5, 21, false) 
setWorldSoundEnabled(5, 22, false) 
setWorldSoundEnabled(5, 23, false) 
setWorldSoundEnabled(5, 24, false) 
setWorldSoundEnabled(5, 26, false) 
setWorldSoundEnabled(5, 27, false) 
setWorldSoundEnabled(5, 29, false) 
setWorldSoundEnabled(5, 32, false) 
setWorldSoundEnabled(5, 33, false) 
setWorldSoundEnabled(5, 52, false) 
setWorldSoundEnabled(5, 55, false) 
setWorldSoundEnabled(5, 63, false) 
setWorldSoundEnabled(5, 73, false) 
setWorldSoundEnabled(5, 76, false) 
setWorldSoundEnabled(5, 83, false) 
end 
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), removeWorldSounds)
----Replaces The Sniper Crosshair
table = {
  -- you can add more stuff here :)
    -- Crosshair
	
    {"files/siterocket.png", "siterocket"},
    {"files/SNIPERcrosshair.png", "SNIPERcrosshair"},

}


addEventHandler( "onClientResourceStart", resourceRoot,
function()
    for i = 1, #table do
        local shader, tec = dxCreateShader ( "texreplace.fx" )
        local tex = dxCreateTexture ( ""..table[i][1].."" )
        engineApplyShaderToWorldTexture ( shader, table[i][2] )
        engineApplyShaderToWorldTexture ( shader, table[i][2].."lod" )
        dxSetShaderValue ( shader, "gTexture", tex )
    end
end)


 

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