Forum

> > CS2D > Scripts > Bad argument (Nil Value)
Forums overviewCS2D overview Scripts overviewLog in to reply

English Bad argument (Nil Value)

13 replies
To the start Previous 1 Next To the start

old Bad argument (Nil Value)

AbAeterno
User Off Offline

Quote
Hello again everyone!
I'm getting the error
1
LUA ERROR: maps/SBV1.0.0.lua:79: Bad Argument #1 to 'msg2' (number expected, got nil)
With this:
Timer that calls the function "timefuncI"
1
timer(2700, "timefuncI")
And here's the function:
1
2
3
4
function timefuncI()
  msg2(id, "©000255000Hello and welcome to our Test Subjects center.") <-- Line 79
  msg2(id, "©000255000As you may have noted, you can now move in another way.")
end

What's wrong here? I think the problem is with the "id" value.

old Re: Bad argument (Nil Value)

AbAeterno
User Off Offline

Quote
Here:
Spoiler >

old Re: Bad argument (Nil Value)

Anders4000
User Off Offline

Quote
You are right. You call the function with no parameters.
Got no fix for you atm. though, I'm not on a computer.

You should add parameters to your functions. Eg:
1
function timefuncI(id)
Do this for your _always and _hit functions too.
edited 1×, last 27.02.12 01:34:42 am

old Re: Bad argument (Nil Value)

J4x
User Off Offline

Quote
Change this
1
2
3
4
function timefuncI()
msg2(id,"©000255000Hello and welcome to our Test Subjects center.")
msg2(id,"©000255000As you may have noted, you can now move in another way.")
end
to this
1
2
3
4
5
6
function timefuncI(i)
for i=1,32 do
msg2(i,"©000255000Hello and welcome to our Test Subjects center.")
msg2(i,"©000255000As you may have noted, you can now move in another way.")
end
end
I'm not good explaining lua, I can just tell the "id" was an undefined variable or something like that...

old Re: Bad argument (Nil Value)

Anders4000
User Off Offline

Quote
user KenVo has written
I think you also have to add:
1
if not player(i,"exists") then return end

under timefuncI()

Yep, i agree.

Without it, the code is kind of sketchy.

old Re: Bad argument (Nil Value)

blood2d
User Off Offline

Quote
is this

1
timer(2700, "timefuncI",id)


1
2
3
4
5
function timefuncI(id) -- this now have a value "id"
msg2(id, "©000255000Hello and welcome to our Test Subjects center.") -- here uses the value "id"
msg2(id, "©000255000As you may have noted, you can now move in another way.")  -- here uses the value "id"
end
end

you don't especifict the id , you in the function timefunc don't type a value , but the msg2 , have a value , is "id"
, ok copy , and paste , and it finish

sorry for my bad english

old Re: Bad argument (Nil Value)

EngiN33R
Moderator Off Offline

Quote
user J4x has written
Change this
1
2
3
4
function timefuncI()
msg2(id,"©000255000Hello and welcome to our Test Subjects center.")
msg2(id,"©000255000As you may have noted, you can now move in another way.")
end
to this
1
2
3
4
5
6
function timefuncI(i)
for i=1,32 do
msg2(i,"©000255000Hello and welcome to our Test Subjects center.")
msg2(i,"©000255000As you may have noted, you can now move in another way.")
end
end
I'm not good explaining lua, I can just tell the "id" was an undefined variable or something like that...


Remember, for efficient scripting - never, EVER get players as

1
for i=1,32 do

Use this construction instead:
1
for _,i in pairs(player(0,"table")) do

It iterates through every value in the player table thus eliminating any possibility of non-existing players, as well as improving the efficiency.

old Re: Bad argument (Nil Value)

EngiN33R
Moderator Off Offline

Quote
By the way, Pwnisher's code is incorrect, now that I think about it. The for loop should be outside the function - if you even need to iterate through the players, because at the moment the function argument does nothing since it's overrided by the inner loop.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview