Forum

> > CS2D > Scripts > Need Help With my Lua Code
Forums overviewCS2D overview Scripts overviewLog in to reply

English Need Help With my Lua Code

5 replies
To the start Previous 1 Next To the start

moved Need Help With my Lua Code

zZGz
User Off Offline

Quote
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...

old Re: Need Help With my Lua Code

Unknown_Soldier
User Off Offline

Quote
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

old Re: Need Help With my Lua Code

Flacko
User Off Offline

Quote
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.

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

old Re: Need Help With my Lua Code

Unknown_Soldier
User Off Offline

Quote
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.

old Re: Need Help With my Lua Code

Flacko
User Off Offline

Quote
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
Log in to reply Scripts overviewCS2D overviewForums overview