Simple Problem, I'm sure.

Hey everyone,

I have just started coding in Lua (I want to make a simple gamemode, eventually), and I have run into a problem when trying to run this simple script:


function buyItem(item)
local money = "$2.00"
if money <= "2.49" then
	Msg("I'm sorry, you don't have enough money to buy that".. item.. ", thank you"
)
	Msg("You only have".. money.. ", sorry"
)
else
	Msg("Here, enjoy your".. item.. ", thank you"
)
end

buyItem("banana")

Basically in GMod it claims there need to be a bracket near a ‘’, even though I can’t see any need for one. Also, I added the local to money, it doesn’t work with it there or without it. (What the difference BTW?)

Thanks, SP.

put the
inside your quotes. os like this



Msg("You only have".. money.. ", sorry
")


If you end a msg with a variable as opposed to a string then you need …
like this



Msg("Here, enjoy your".. item.. 
)


Ok, thanks :slight_smile:

[editline]01:40AM[/editline]

Mmm… First I ran the script how you said too, and I got this:



] lua_openscript testBananaBuy.lua
Running script testBananaBuy.lua...
lua	estbananabuy.lua:10: 'end' expected (to close 'function' at line 1) near '<eof>'


So, I added end after my final function calling, and I got this:


] lua_openscript testBananaBuy.lua
Running script testBananaBuy.lua...

Which doesn’t end. Any ideas?
Thanks.

Please tell me you didn’t put the final end after “buyItem(“banana”)”, else you will have created either an endless recursive function, or a script that declares an endless recursive function but never calls it. Either way, it wouldn’t work.

Also, you are using a string with the -sign, yet when you check the value of money, you use a string without the -sign. There are many things wrong with this, so I will just suggest that you switch to using numbers to avoid all of this nonsense (PS: using the < operator won’t work the way you want it to when yo are comparing strings).

Oh god I didn’t even notice that.
Ya when dealing with money never use strings always use integers. The when you need to print it out just put the $ sign there.

O.K., thanks everyone. Much appreciated. I am just beginning, but these are sloppy mistakes.

Thanks.

[editline]09:07AM[/editline]

O.K, I think I solved those problems, and I’ve changed it so it’s >=, not <=. If this is still wrong, can you please elaborate?

I keep getting the <eof> error at line 1. Here is my new code:


function buyItem(item)
local money = "2.00"
if money >= "2.50" then
	Msg("Here, enjoy your".. item.. ", thank you
")
else	
	Msg("I'm sorry, you don't have enough money to buy that".. item.. ", thank you
")
	Msg("You only have".. money.. ", sorry
")
end

buyItem("banana")


Ideas?

[editline]10:18AM[/editline]

Bump

[lua]function buyItem(item)

local money = 2.00

if money &gt;= 2.49 then
	Msg("I'm sorry, you don't have enough money to buy that".. item.. ", thank you

")
Msg(“You only have”… money… ", sorry
")
else
Msg(“Here, enjoy your”… item… ", thank you
")
buyItem(“banana”)
end

end[/lua]

Try that.

Edit:

I also improved it a little bit to subtract the amount from the money.

[lua]function buyItem(item)

local money = 2.00
local cost = 2.49

if money &gt;= cost then
	Msg("I'm sorry, you don't have enough money to buy that".. item.. ", thank you"

)
Msg(“You only have”… money… ", sorry
")
else
Msg(“Here, enjoy your”… item… ", thank you
")
money = money - cost
buyItem(“banana”)
end

end[/lua]

Haha, thanks very much :slight_smile: I am glad you’re willing to help noobs like myself.

Out of curiosity, how did yourself, and how do most other people go around learning Lua? Currently I am doing basic Lua tutorials and the gamemode tutorials (from scratch), but I am not sure whether I’m taking the right aproach…

Thanks again.

[editline]11:00AM[/editline]

Mm, I just tried the scripts, and it ends up in the endless ‘Running Script ***’ state. Any ideas?

I myself learned lua multiple ways, I had a friend who was teaching me lua at the time, and another friend who I was learning lua with, and I still am now, in fact, i’m teaching him.

But to start off, you probably would want to go no bigger than a small derma menu, just displaying the simple vgui elements, to make sure you get the hang on position relative to screen, and size and layout.

That’s how I started off, then creating simple SWEP’s that do different things depending on your level of imagination, and what you are capable of.

I then went on to make a simple admin modification ( not a release worthy one, just a simple test for myself to learn from ) using DComboBox and the for loop to include players, this also got me used to using usermessages and console commands.

I then created an exp system based on kills, and how well you are achieving, this was pretty easy, using the HUD to display key information for it all, including an EXP bar, this got me used to using the math strings such as math.clamp, math.floor and math.fmod, but much more as well.

It’s really simple to do stuff in lua if you know how to, but some people have a different learning curve, based on what you want to be able to do.

Hope that was helpful, have a fun time coding :smiley:

[editline]11:09AM[/editline]

It’s probably because you do not have anything running the function, like a derma menu or something? just create a derma menu, and create a button, and then do:

[lua]DButton1.DoClick = function()
buyItem(“banana”)
end[/lua]

The numbers shouldnt be in “”'s, thus making them strings

Ah crap, newbie mistake

Fixed now*

That’s awesome now, thanks :slight_smile: And thanks for your advice and motivation, much appreciation.

But there are a few things I am still uncertain about in Lua, like why this tutorial man suddenly does ‘team.SetUp’ is this a function, variable or what, and what is the use of local?

Thanks! :slight_smile:

:science:

Anything that ends with () is a function (or a method which is also a function). If you’re unsure what a function does look it up on the wiki by either searching for it or browsing the function reference. As for locals have a read of “Programming in lua” in the link I gave you.

team.SetUp lets you create teams in a gamemode, such as two opposing sides.

Thanks, so what is it? A function?

I’d call it a function yes

O.K, thanks again xD

And what about the use/not use of local when declaring variables?

Read my earlier post, but it has to do with scoping. With where that value can be accessed from. A local inside of a function can’t be accessed from outside of it. A global(anything not a local) can be accessed from anywhere, even another script. So as a general rule you local everything unless you need to access it from somewhere else.

Thanks again :slight_smile:

[editline]02:20PM[/editline]

Err, I have another problem. When I try to set the model for a character in my test gamemode, it just uses my usual zombie model which I’ve set somewhere else.



function GM:PlayerLoadout( ply ) 
 
	ply:StripWeapons() 
 
	if ply:Team() == 1 then
		ply:Give("weapon_physgun") 
		ply:SetModel( "models/player/barney.mdl" )


Any ideas? Thanks…

SetModel only applies after a player respawns.