Jump to content

help with tables


DaaNN

Recommended Posts

Hello, I have a small error with an operation that I still don't understand, when I try to use the /subrange command, it gives me an error, the ID returns a boolean and I have no idea, I tried to use tonumber(ID) or 0, it works but no It increases the ranks, it only keeps the 0.

 

My intention with this is to add ID + 1, and add 1 + 1 until reaching the maximum of the table.

 

local _Rangos = {

	["Policia"]={
    'Cadete'
    ,'Oficial I'
    ,'Oficial II'
    ,'Oficial III'
    ,'Oficial III+'
    ,'Detective I'
    ,'Detective II'
    ,'Sargento'
    ,'Teniente'
    ,'Capitan'
	},

	["Medico"]={
     'Aspirante'
    ,'Paramedico'
    ,'Medico'
    ,'Rescatista'
    ,'Sargento'
    ,'Director'
	},
	
	["Mecanico"]={
    'Aprendiz'
    ,'Junior'
    ,'Mecanico'
    ,'Mecanico Experto'	
    ,"Sub Empresario"	
	},
}

function table.find(t, value)
    for k,v in ipairs(t) do
      	  if v == value then
            return k,v
      	end
    end
    return false
end

function _Cmd_Rangos(jug, cmd, name)
	if not notIsGuest( jug ) then
		if jug:getData("Roleplay:faccion") ~= "" then
			if jug:getData("Roleplay:faccion_rango") == "Capitan" or jug:getData("Roleplay:faccion_rango") == "Comandante" or jug:getData("Roleplay:faccion_rango") == "Director" or jug:getData("Roleplay:faccion_rango") == "Sub Director" or jug:getData("Roleplay:faccion_rango") == "Empresario" or jug:getData("Roleplay:faccion_rango") == "Sub Empresario"  or jug:getData("Roleplay:faccion_rango") == "Jefe de Bomberos" then
		        local who = exports["Gamemode"]:getFromName( jug, name )
		        if who then
		            local old_rank = who:getData("Roleplay:faccion_rango") or false
		            if old_rank then
		                local ID = table.find(_Rangos[jug:getData("Roleplay:faccion")], old_rank)
		                if ID then
		                    if cmd == 'subirrango' then
		                        ID = ID + 1
		                    elseif cmd == 'bajarrango' then
		                        ID = ID - 1
		                    end
		                    if _Rangos[jug:getData("Roleplay:faccion")][ID] then
		                        if who then
		                        	update("UPDATE Facciones SET Rango = ?  WHERE Nombre = ?", _Rangos[who:getData("Roleplay:faccion")][ID], AccountName(who))
		                            who:setData("Roleplay:faccion_rango", _Rangos[jug:getData("Roleplay:faccion")][ID])

		                            who_sms = (cmd == 'subirrango' and jug.name..' te ascendió a '.._Rangos[jug:getData("Roleplay:faccion")][ID]) or (cmd == 'bajarrango' and jug.name..' te a bajado de puesto a '.._Rangos[jug:getData("Roleplay:faccion")][ID])
		                            who:outputChat(who_sms,(cmd == 'subirrango' and 0 or 255),(cmd == 'subirrango' and 255 or 0),0)

		                            jug_sms = (cmd == 'subirrango' and "Ascendiste a "..who:getName().." al rango ".._Rangos[jug:getData("Roleplay:faccion")][ID]) or (cmd == 'bajarrango' and who:getName().." Bajo al rango ".._Rangos[jug:getData("Roleplay:faccion")][ID])
		                            jug:outputChat(jug_sms,(cmd == 'subirrango' and 0 or 255),(cmd == 'subirrango' and 255 or 0),0)
		                        end
		                    end
		                end
		            end
		        else
		        	jug:outputChat("Syntax: /subirrango [ID]", 255, 255, 255)
		        	jug:outputChat("Syntax: /bajarrango [ID]", 255, 255, 255)
		        end
			end
		end
	end
end
addCommandHandler('subirrango',_Cmd_Rangos)
addCommandHandler('bajarrango',_Cmd_Rangos)

 

 

The error is the following:
attempt to perform arithmetic on local "ID" (a boolean value)

What I can do?

Link to comment
  • Moderators

Are you sure you are showing the code where it actually happens ?

Because there is no way you can reach the arithmetic operation (+1 or -1) unless ID = true, but table.find can't return true, it can only return false so if ID then should stop the code there.

Can you show the exact command you type in chat, and the exact error message please ? (and which line the error occurs at)

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