" 'for' limit must be a number " - LUA script


--[[ 		Globals		]]
playerTimer = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

--[[ 		Code		]]

function Drawer()
local counter = 1
	for i = 1, count, 1 do
	local object = lol.GetPlayer(i)
		if object.team ~= player.team and object.team ~= TEAM_NONE and object.dead == 0 then
			if object.visible == 1 then
				if playerTimer* ~= 0 then
					playerTimer* = 0
					lol.DrawText(string.format("%s",object.charName),WINDOW_W - 90,counter*10,1,1,1,1);
				end
			end
		end
	end
end

function Timer( tick )
	local count = lol.GetPlayerCount()
	for i = 1, count, 1 do
		local object = lol.GetPlayer(i)
		if object.team ~= player.team and object.team ~= TEAM_NONE and object.dead == 0 then
			if object.visible == 1 then
				if printChat ~= 0 and playerTimer* == 0 then
				playerTimer[ i ] = tick
				end
			end
		end
	end
end


function Load()
	system.SetDrawCallback("Drawer")
	system.SetTimerCallback("Timer",500)
	lol.PrintChat(" >> Enemy Miss loaded!")
end

Problem is in the Drawer function, which is supposed to be the callback to draw from.
It says ‘for’ limit must be a number. o.O.
Any help is appreciated, thanks!

The variable ‘count’ is not defined. Did you mean to use ‘counter’? (line 7-8)

[editline]30th December 2011[/editline]

Oh, I see you defined it locally in the Timer function. Either pass it as an argument to the function, redefine it in the Drawer function or make it a global (not recommended).

Nope, counter is used in drawing, so that each time there’s an overlap, the drawing doesn’t overlap, it draws above it.
I want to make a loop for i, to check for all players who’re positive to continue the search ( if ).
What else can I use instead of count? no foreach here :frowning:

[editline]30th December 2011[/editline]

Can you give me an example? Thank you.

Use ‘pairs’:

[lua]
for k, v in pairs(player.GetAll()) do – would go through all players
pl:Kill()
end
[/lua]

**[G.pairs

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.pairs)**

Would this go through all the players?

[editline]30th December 2011[/editline]

And how do I use k,v instead of i?

Loops through all the elements returned by the input table (in this case what is returned by player.GetAll()). That would be all valid players.

You can still use your if statement inside of the loop.

It would look like this :


function Drawer()
local counter = 1
for k,v in pairs( player.GetAll() ) do
	local object = lol.GetPlayer(v)
		if object.team ~= player.team and object.team ~= TEAM_NONE and object.dead == 0 then
			if object.visible == 1 then
				if playerTimer[v] ~= 0 then
					playerTimer[v] = 0
					lol.DrawText(string.format("%s",object.charName),WINDOW_W - 90,counter*10,1,1,1,1);
				end
			end
		end
	end
end

?

I guess; the loop itself looks okay to me.

More info on ‘pairs’:

It is pretty much the Lua-equivalent of the PHP foreach:


foreach ($arr as $key => $value) {
    
}

[lua]
for key, value in pairs(array) do

end
[/lua]

Another error…
“attempt to call field ‘GetAll’ (a number value)”

Show us some code; the error is not coming from the last snippet you posted…



--[[ 		Globals		]]
playerTimer = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

--[[ 		Code		]]

function Drawer()
local counter = 1
for k,v in pairs( lol.GetPlayerCount() ) do
	local object = lol.GetPlayer(v)
		if object.team ~= player.team and object.team ~= TEAM_NONE and object.dead == 0 then
			if object.visible == 1 then
				if playerTimer[v] ~= 0 then
					playerTimer[v] = 0
					lol.DrawText(string.format("%s",object.charName),WINDOW_W - 90,counter*10,1,1,1,1);
				end
			end
		end
	end
end

function Timer( tick )
	local count = lol.GetPlayerCount()
	for i = 1, count, 1 do
		local object = lol.GetPlayer(i)
		if object.team ~= player.team and object.team ~= TEAM_NONE and object.dead == 0 then
			if object.visible == 1 then
				if printChat ~= 0 and playerTimer* == 0 then
				playerTimer[ i ] = tick
				end
			end
		end
	end
end


function Load()
	system.SetDrawCallback("Drawer")
	system.SetTimerCallback("Timer",500)
	lol.PrintChat(" >> Enemy Miss loaded!")
end


Edited ‘GetAll’, I’m using this code for another game other than GMod.

[editline]30th December 2011[/editline]

Okay, change it now to :



--[[ 		Globals		]]
playerTimer = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

--[[ 		Code		]]

function Drawer()
local count = lol.GetPlayerCount()
local counter = 1
	for i = 1, count, 1 do
	local object = lol.GetPlayer(i)
		if object.team ~= player.team and object.team ~= TEAM_NONE and object.dead == 0 then
			if object.visible == 1 then
				if playerTimer* ~= 0 then
					playerTimer* = 0
					lol.DrawText(string.format("%s",object.charName),WINDOW_W - 90,counter*10,1,1,1,1);
				end
			end
		end
	end
end

function Timer( tick )
	local count = lol.GetPlayerCount()
	for i = 1, count, 1 do
		local object = lol.GetPlayer(i)
		if object.team ~= player.team and object.team ~= TEAM_NONE and object.dead == 0 then
			if object.visible == 1 then
				if printChat ~= 0 and playerTimer* == 0 then
				playerTimer[ i ] = tick
				end
			end
		end
	end
end


function Load()
	system.SetDrawCallback("Drawer")
	system.SetTimerCallback("Timer",500)
	lol.PrintChat(" >> Enemy Miss loaded!")
end


The player library is exclusive to gLua. This is the Garry’s Mod Lua section, though. You would be better off asking in the forum for that particular game.

Sadly, no other forum has anything for that particular game.
And no other forum has any support for Lua, so I’m stuck here.
It doesn’t have much of a different library though, so most of the code is the same.

[editline]30th December 2011[/editline]

Fixed, thank you.