Jump to content

Help attemp global index


VenomOG

Recommended Posts

or ind,val in ipairs(levels) do
		if ind > currank and val.reach <= exp then
			rank = ind
			arank = tonumber(rank)+1
			areach = val[arank]
			areach = areach.reach
			oreach = val.reach
			newrank = true
		end
	end

---------
rank,reach,oreach = getRankForEXP(plr,"Level",new)
		setElementData(plr,"Level",rank)
		setElementData(plr, "LevelXP", reach)
		setElementData(plr, "oLevelXPreach", oreach)

The problem is areach , it is like it doesn't define.

Link to comment

The snippet you've posted seems to be so carelessly copied and pasted it doesn't look valid. I can take a couple guesses such as 'or' actually being 'for', etc. but it would be better if you posted a more complete example, including the definition of the levels table, and the definition of getRankForEXP (if it's not the code right above, stripped of it's function definition line)

Link to comment

Its just to protect the code, you want the full one?
 

levels = {
	{ reach = 0 },
	{ reach = 50 },
	{ reach = 100 },
	{ reach = 180 },
	{ reach = 240 },
	{ reach = 300 },
	{ reach = 370 },
	{ reach = 450 },
	{ reach = 560 },
	{ reach = 690 },
	{ reach = 800 },
	{ reach = 1000 },
	{ reach = 1400 },
	{ reach = 1600 },
	{ reach = 1700 },
	{ reach = 2000 },
	{ reach = 3000 },
	{ reach = 4000 },
	{ reach = 5000 },	
}
function getRankForEXP(plr,exp)
newrank = false
	currank = tonumber(getElementData(plr,"Level")) or 0
	curreach = tonumber(getElementData(plr,"LevelXP")) or 100
	curoreach = tonumber(getElementData(plr,"oLevelXPReach")) or 0

	for ind,val in ipairs(levels) do
		if ind > currank and val.reach <= exp then
			rank = ind
			arank = tonumber(rank)+1
			areach = levels[arank]
			areach = areach.reach
			oreach = val.reach
			newrank = true
		end
	end
	if newrank then
		return rank,areach,oreach
		end
				return currank, curreach, curoreach

end

@MrTasty

Link to comment
rank = ind
arank = tonumber(rank)+1
areach = levels[arank] -- arank is not guarenteed to be a valid index, thereby returning a nil value
oreach = val.reach -- and thus this line would fail by attempting to index such nil value

You need to check if whether the player is at the last level before trying to calculate areach. Also, you should definitely look into using local variables more, it's inefficient to use global variables.

function getRankForEXP(plr,exp)
  local newrank = false
  local currank = tonumber(getElementData(plr,"Level")) or 0
  local curreach = tonumber(getElementData(plr,"LevelXP")) or 100
  local curoreach = tonumber(getElementData(plr,"oLevelXPReach")) or 0

  local rank, areach, oreach -- declare these local in this scope, so when they're written to they don't use _G but rather upvalues
  for ind,val in ipairs(levels) do
    if ind > currank and val.reach <= exp then
      rank = ind
      areach = levels[ind+1] -- optimised this a little
      if areach then
        areach = areach.reach
      else
        -- TODO: final level, no areach
      end
      oreach = val.reach
      newrank = true
    end
  end
  if newrank then
    return rank,areach,oreach
  end
  return currank, curreach, curoreach
end

 

Edited by MrTasty
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...