English Bad argument (Nil Value)

13 replies
Goto Page
To the start Previous 1 Next To the start
27.02.12 12:45:21 am
Up
AbAeterno
User
Offline Off
Hello again everyone!
I'm getting the error
Code:
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"
Code:
1
timer(2700, "timefuncI")

And here's the function:
Code:
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.
27.02.12 12:51:48 am
Up
J4x
User
Offline Off
Show the whole script.
27.02.12 12:54:49 am
Up
AbAeterno
User
Offline Off
Here:
Spoiler >
27.02.12 01:29:28 am
Up
Anders4000
User
Offline Off
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:
Code:
1
function timefuncI(id)

Do this for your _always and _hit functions too.
edited 1×, last 27.02.12 01:34:42 am
27.02.12 01:29:55 am
Up
J4x
User
Offline Off
Change this
Code:
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
Code:
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...
27.02.12 01:38:36 am
Up
AbAeterno
User
Offline Off
@user J4x Thank you! This worked. I now also kinda know how the "for...do" works. Thanks.
27.02.12 02:00:57 am
Up
KenVo
User
Offline Off
I think you also have to add:
Code:
1
if not player(i,"exists") then return end


under timefuncI()
27.02.12 08:40:34 am
Up
Anders4000
User
Offline Off
user KenVo has written:
I think you also have to add:
Code:
1
if not player(i,"exists") then return end


under timefuncI()

Yep, i agree.

Without it, the code is kind of sketchy.
27.02.12 09:08:55 am
Up
blood2d
User
Offline Off
is this

Code:
1
timer(2700, "timefuncI",id)



Code:
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
27.02.12 01:29:04 pm
Up
EngiN33R
Moderator
Offline Off
user J4x has written:
Change this
Code:
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
Code:
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

Code:
1
for i=1,32 do


Use this construction instead:
Code:
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.
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
27.02.12 10:51:15 pm
Up
J4x
User
Offline Off
@user EngiN33R: I didn't know that, thanks for the tip.
27.02.12 11:01:26 pm
Up
Infinite Rain
Reviewer
Offline Off
Why can't you just use msg function except of msg2 with annoying cycle?
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
27.02.12 11:12:51 pm
Up
AbAeterno
User
Offline Off
Because without msg2, it will show the msg to every player, that's not what I want.
27.02.12 11:24:18 pm
Up
EngiN33R
Moderator
Offline Off
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.
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
To the start Previous 1 Next To the start