Forum

> > CS2D > Scripts > Need Help With my Lua Code
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Need Help With my Lua Code

5 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

verschoben Need Help With my Lua Code

zZGz
User Off Offline

Zitieren
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 Kommentar

you need help with choosing a decent title...

alt Re: Need Help With my Lua Code

Unknown_Soldier
User Off Offline

Zitieren
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

alt Re: Need Help With my Lua Code

Flacko
User Off Offline

Zitieren
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

alt Re: Need Help With my Lua Code

Unknown_Soldier
User Off Offline

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

alt Re: Need Help With my Lua Code

Flacko
User Off Offline

Zitieren
Before anything,
Unknown_Soldier hat geschrieben
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 hat geschrieben
What I have learned: never use "player" as a function parameter

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

Flacko hat geschrieben
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.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht