• Simple Problem, I'm sure.
    33 replies, posted
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: [code]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"\n) Msg("You only have".. money.. ", sorry"\n) else Msg("Here, enjoy your".. item.. ", thank you"\n) end buyItem("banana")[/code] 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 \n inside your quotes. os like this [code] Msg("You only have".. money.. ", sorry\n") [/code] If you end a msg with a variable as opposed to a string then you need .. \n like this [code] Msg("Here, enjoy your".. item.. \n) [/code]
Ok, thanks :) [editline]01:40AM[/editline] Mmm... First I ran the script how you said too, and I got this: [code] ] lua_openscript testBananaBuy.lua Running script testBananaBuy.lua... lua\testbananabuy.lua:10: 'end' expected (to close 'function' at line 1) near '<eof>' [/code] So, I added end after my final function calling, and I got this: [code]] lua_openscript testBananaBuy.lua Running script testBananaBuy.lua...[/code] 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: [code]function buyItem(item) local money = "2.00" if money >= "2.50" then Msg("Here, enjoy your".. item.. ", thank you\n") else Msg("I'm sorry, you don't have enough money to buy that".. item.. ", thank you\n") Msg("You only have".. money.. ", sorry\n") end buyItem("banana") [/code] Ideas? [editline]10:18AM[/editline] Bump
[lua]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\n") Msg("You only have".. money.. ", sorry\n") else Msg("Here, enjoy your".. item.. ", thank you\n") buyItem("banana") end end[/lua] Try that. [B]Edit:[/B] 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 >= cost then Msg("I'm sorry, you don't have enough money to buy that".. item.. ", thank you"\n) Msg("You only have".. money.. ", sorry\n") else Msg("Here, enjoy your".. item.. ", thank you\n") money = money - cost buyItem("banana") end end[/lua]
Haha, thanks very much :) 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 :D [editline]11:09AM[/editline] [QUOTE=StapledPuppet;23755268]Mm, I just tried the scripts, and it ends up in the endless 'Running Script ***' state. Any ideas?[/QUOTE] 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
[QUOTE=Willox;23755745]The numbers shouldnt be in ""'s, thus making them strings[/QUOTE] Ah crap, newbie mistake Fixed now*
That's awesome now, thanks :) 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! :)
[url]http://wiki.garrysmod.com/?title=Lua[/url] :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.
[QUOTE=StapledPuppet;23756671]That's awesome now, thanks :) 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! :)[/QUOTE] 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 :) [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. [code] function GM:PlayerLoadout( ply ) ply:StripWeapons() if ply:Team() == 1 then ply:Give("weapon_physgun") ply:SetModel( "models/player/barney.mdl" ) [/code] Any ideas? Thanks...
SetModel only applies after a player respawns.
[lua]function GM:PlayerLoadout( ply ) ply:StripWeapons() if ply:Team() == 1 then ply:Give("weapon_physgun") ply:SetModel( "models/player/barney.mdl" ) end end[/lua]
I type kill in the console, and it still shows mes as a zombie...?
[lua]function GM:PlayerLoadout( ply ) ply:StripWeapons() if ply:Team() == 1 then ply:Give("weapon_physgun") ply:SetModel( "models/player/barney.mdl" ) ply:Spawn() end end[/lua]
Oh yeah xD Thanks! :P [editline]02:35PM[/editline] O.K, I did that, went on, chose my team, killed myself, and it still shows a zombie...? Do I need to put the .mdl s in my content folder? [editline]02:37PM[/editline] @ Jackthemaster, I cut the code out of a larger piece witht he required amount of ends :) Thanks anyway. [editline]02:39PM[/editline] And I forgot to mention I'm running this on my own server... =/
Are you sure you have not put [lua]function GM:PlayerSpawn( ply ) ply:SetModel('models/player/zombie_classic.mdl') end[/lua] Somewhere in your code?
I've used lots from a tutorial, and it's surprisingly helped me a lot with Lua... Here's my code for init.lua [code] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( 'shared.lua' ) function GM:PlayerInitialSpawn( ply ) ply:ConCommand( "team_menu" ) end function GM:PlayerLoadout( ply ) ply:StripWeapons() if ply:Team() == 1 then ply:Give("weapon_physgun") ply:SetModel( "models/player/barney.mdl" ) ply:Spawn() elseif ply:Team() == 2 then ply:Give("weapon_smg1") ply:GiveAmmo( 240, "smg1" ) ply:SetModel( "models/player/gman_high.mdl" ) ply:Spawn() end end function team_1( ply ) ply:SetTeam( 1 ) ply:Spawn() end function team_2( ply ) ply:SetTeam( 2 ) ply:Spawn() end concommand.Add( "team_1", team_1 ) concommand.Add( "team_2", team_2 )[/code]
[b][url=wiki.garrysmod.com/?title=Gamemode.PlayerSetModel]Gamemode.PlayerSetModel [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] or.... maybe not
Haha, thanks very much :) [editline]04:07PM[/editline] But how do I do that on spawn?
If you want to do something on spawn hook it to [b][url=wiki.garrysmod.com/?title=Gamemode.PlayerSpawn]Gamemode.PlayerSpawn [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] if you want to do something ONLY the first time they spawn ( e.g. when they join the server ) then use [b][url=wiki.garrysmod.com/?title=Gamemode.PlayerInitialSpawn]Gamemode.PlayerInitialSpawn [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
Thanks, I've solved it now :)
Sorry, you need to Log In to post a reply to this thread.