Jump to content

my code is make lag


Recommended Posts

My code is working 100%, but it makes lag once I switch weapon..

take a look

function Switch(button,press)
wep = getPedWeaponSlot(localPlayer)
    a= {}
for z=0,12 do
    a[z] = exports.settingPanel:getSetting(tostring(z))
end
if not a then return end

    for z= 0,12 do
            if button == a[0] then
                if(press) then
                    setPedWeaponSlot(localPlayer, 0)
                end
            end    
                if a[0] == a[z+1] then
                    addEventHandler("onClientKey", root, function (button, press)
                        if button == a[0] then    
                                if (press) then
                                    if wep == 0 then
                                        setPedWeaponSlot(localPlayer, z+1)                                
                                    else
                                        setPedWeaponSlot(localPlayer,0)                                
                                    end                                
                                end
                            
                        end
                    end)
                end
    end
    for z= 0,12 do
            if button == a[2] then
                if(press) then
                    setPedWeaponSlot(localPlayer, 2)
                end
            end    
                if a[2] == a[z+3] then
                    addEventHandler("onClientKey", root, function (button, press)
                        if button == a[2] then    
                                if (press) then
                                    if wep == 2 then
                                        setPedWeaponSlot(localPlayer, z+3)                                
                                    else
                                        setPedWeaponSlot(localPlayer,2)                                
                                    end                                
                                end
                            
                        end
                    end)
                end
    end
    for z= 0,12 do
            if button == a[3] then
                if(press) then
                    setPedWeaponSlot(localPlayer, 3)
                end
            end    
                if a[3] == a[z+4] then
                    addEventHandler("onClientKey", root, function (button, press)
                        if button == a[3] then    
                                if (press) then
                                    if wep == 3 then
                                        setPedWeaponSlot(localPlayer, z+4)                                
                                    else
                                        setPedWeaponSlot(localPlayer,3)                                
                                    end                                
                                end
                            
                        end
                    end)
                end
    end
    for z= 0,12 do
            if button == a[4] then
                if(press) then
                    setPedWeaponSlot(localPlayer, 4)
                end
            end    
                if a[4] == a[z+5] then
                    addEventHandler("onClientKey", root, function (button, press)
                        if button == a[4] then    
                                if (press) then
                                    if wep == 4 then
                                        setPedWeaponSlot(localPlayer, z+5)                                
                                    else
                                        setPedWeaponSlot(localPlayer,4)                                
                                    end                                
                                end
                            
                        end
                    end)
                end
    end
    for z= 0,12 do
            if button == a[5] then
                if(press) then
                    setPedWeaponSlot(localPlayer, 5)
                end
            end    
                if a[5] == a[z+6] then
                    addEventHandler("onClientKey", root, function (button, press)
                        if button == a[5] then    
                                if (press) then
                                    if wep == 5 then
                                        setPedWeaponSlot(localPlayer, z+6)                                
                                    else
                                        setPedWeaponSlot(localPlayer,5)                                
                                    end                                
                                end
                            
                        end
                    end)
                end
    end
    for z= 0,12 do
            if button == a[6] then
                if(press) then
                    setPedWeaponSlot(localPlayer, 6)
                end
            end    
                if a[6] == a[z+7] then
                    addEventHandler("onClientKey", root, function (button, press)
                        if button == a[6] then    
                                if (press) then
                                    if wep == 6 then
                                        setPedWeaponSlot(localPlayer, z+7)                                
                                    else
                                        setPedWeaponSlot(localPlayer,6)                                
                                    end                                
                                end
                            
                        end
                    end)
                end
    end
    for z= 0,12 do
            if button == a[7] then
                if(press) then
                    setPedWeaponSlot(localPlayer, 7)
                end
            end    
                if a[7] == a[z+8] then
                    addEventHandler("onClientKey", root, function (button, press)
                        if button == a[7] then    
                                if (press) then
                                    if wep ==7 then
                                        setPedWeaponSlot(localPlayer, z+8)                                
                                    else
                                        setPedWeaponSlot(localPlayer,7)                                
                                    end                                
                                end
                            
                        end
                    end)
                end
    end
    for z= 0,12 do
            if button == a[8] then
                if(press) then
                    setPedWeaponSlot(localPlayer, 8)
                end
            end    
                if a[8] == a[z+9] then
                    addEventHandler("onClientKey", root, function (button, press)
                        if button == a[8] then    
                                if (press) then
                                    if wep ==8 then
                                        setPedWeaponSlot(localPlayer, z+9)                                
                                    else
                                        setPedWeaponSlot(localPlayer,8)                                
                                    end                                
                                end
                            
                        end
                    end)
                end
  end    
end
addEventHandler("onClientKey", root, Switch)

 

Link to comment
  • Moderators

Every time you press a key.

function Switch(button,press)
  -- yes here
end
addEventHandler("onClientKey", root, Switch)

=

You create a new function/functions with an addEventHandler attached to it.

 

With other words, you created a memory leak by doing this:

addEventHandler("onClientKey", root, function (button, press)
	if button == a[0] then    
		if (press) then
			if wep == 0 then
				setPedWeaponSlot(localPlayer, z+1)                                
			else
				setPedWeaponSlot(localPlayer,0)                                
			end                                
		end
	end
end)

 

For every next click you execute those addEventHandlers * times you clicked. The longer you are doing this, the more lag it creates.

 


 

You are having 8 loops that execute 13 times. 8 * 13 = 104 looping.

for z= 0,12 do
  -- yes this 8 times.
end

How about you just use 1? :x

 


 

 

I do not even know how it is suppose to work, so I am not going to rewrite your code.

What I do recommend you to is write very clear how your code is suppose to function. And write down which steps the code has to take to achieve it's goal.

 

>>> Writing a new unknown functionality without defining it's steps is bad practice. (Unless you have written something similar before) <<< Not everybody agrees to this, that is fine.

 

 

 

 

 

Edited by IIYAMA
Link to comment
On 8/13/2018 at 11:25, IIYAMA said:

Every time you press a key.


function Switch(button,press)
  -- yes here
end
addEventHandler("onClientKey", root, Switch)

=

You create a new function/functions with an addEventHandler attached to it.

 

With other words, you created a memory leak by doing this:


addEventHandler("onClientKey", root, function (button, press)
	if button == a[0] then    
		if (press) then
			if wep == 0 then
				setPedWeaponSlot(localPlayer, z+1)                                
			else
				setPedWeaponSlot(localPlayer,0)                                
			end                                
		end
	end
end)

 

For every next click you execute those addEventHandlers * times you clicked. The longer you are doing this, the more lag it creates.

 


 

You are having 8 loops that execute 13 times. 8 * 13 = 104 looping.


for z= 0,12 do
  -- yes this 8 times.
end

How about you just use 1? :x

 


 

 

I do not even know how it is suppose to work, so I am not going to rewrite your code.

What I do recommend you to is write very clear how your code is suppose to function. And write down which steps the code has to take to achieve it's goal.

 

>>> Writing a new unknown functionality without defining it's steps is bad practice. (Unless you have written something similar before) <<< Not everybody agrees to this, that is fine.

 

 

 

 

 

If you know CIT binds script,  weapon switch system with 2 slots in 1 bind, thats my code, if you can fix it please do it.

Link to comment
  • Moderators
3 hours ago, TheRobot2144 said:

If you know CIT binds script,  weapon switch system with 2 slots in 1 bind, thats my code, if you can fix it please do it.

I am sorry, I do not play CIT.

 

Please make some of your own adjustments first based on my feedback, before you give it to somebody else. That is the least thing you can do in order to understand your own mistakes.

Link to comment
On 8/14/2018 at 08:14, IIYAMA said:

I am sorry, I do not play CIT.

 

Please make some of your own adjustments first based on my feedback, before you give it to somebody else. That is the least thing you can do in order to understand your own mistakes.

I would have done it if I understood you 

Link to comment
  • Moderators
10 hours ago, TheRobot2144 said:

I would have done it if I understood you 

Reducing 8 similar loops to 1 loop is unclear? Really?

Momentarily you are giving me the feeling that you do not understand your own script, which is a big issue.

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