English Need Help With my Lua Code

5 replies
Goto Page
To the start Previous 1 Next To the start
07.03.11 02:41:17 am
Up
zZGz
User
Offline Off
addhook("say","rpmenu")

function rpmenu(player, text)
     if(text == "!buyhealth")then
          if(money > 10)then
               msg("MIRACLES!")
          end
     end
end

How do I get it to check a players money, then say "MIRACLES!"

The miracles part is a place holder to add 10 health to player.

Admin/mod comment:

you need help with choosing a decent title...
07.03.11 04:57:10 am
Up
Unknown_Soldier
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
addhook("say","rpgmenu")
function rpmenu(player,text)
     if(text=="!buyhealth")then
          if(player(player,"money"))then
               msg("MIRACLES!")
               parse("sethealth "..player.." "..player(player,"health")+10)
parse("setmoney "..player.." "..player(player,"money")-10)
          end
     end
end


I guess you substracts 10 of money
Should I come back and make one last map for CS2D?
07.03.11 02:58:02 pm
Up
Flacko
User
Offline Off
That won't work. You're naming a function parameter just like the global function "player", freaking bad, Lua will try to call the local variable instead which will cause an error to be printed in console.

Code:
1
2
3
4
5
6
7
8
9
10
function rpmenu(pl, txt)
     if text == "!buyhealth" then
          local money = player(pl,"money")
          if money > 10 then
               parse("setmoney "..pl.." "..money-10)
               parse("sethealth "..pl.." "..player(pl,"health")+10)
               msg("MIRACLES!")
          end
     end
end
07.03.11 03:38:17 pm
Up
Surplus
User
Offline Off
Bad title.
Thats what ima gona say first
07.03.11 06:37:07 pm
Up
Unknown_Soldier
User
Offline Off
Sry, I just modified his lua to fix it, that is why I used "player" as a function parameter, I usually use "id" for players

What I have learned: never use "player" as a function parameter

btw flacko, I can make complexs scripts, I just make lots of mistakes before the right code.
Should I come back and make one last map for CS2D?
08.03.11 02:40:26 am
Up
Flacko
User
Offline Off
Before anything,
Unknown_Soldier has written:
btw flacko, I can make complexs scripts, I just make lots of mistakes before the right code.

I don't care at all.

Unknown_Soldier has written:
What I have learned: never use "player" as a function parameter

In that case you haven't understood what I meant.

Flacko has written:
You're naming a function parameter just like the global function "player", freaking bad

You must NEVER name ANY variable (not only parameters) like a global function/variable.

By global CS2D function I mean player, parse, msg, msg2, print, addhook, menu, timer, image, game, map, etc.

If you name any global variable like that, you will lose the original function and you won't be able to access it again.

And if you name a local variable (or parameter) like one of the default functions you won't be able to access it again neither in the current scope nor in any of the inner scopes.
However, you will be able to use the original values once you get out of scope in this case.
To the start Previous 1 Next To the start