Author Topic: Need some help with lua! :)  (Read 3512 times)

0 Members and 1 Guest are viewing this topic.

Offline slowtech

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Need some help with lua! :)
« on: August 20, 2010, 07:05:09 PM »
I would like to have it notified in chat when someone goes in noclip.
So i thought of making this, in fun.lua?
Code: [Select]
------------------------------Noclip -----------------------------
if v:GetMoveType() == MOVETYPE_WALK then
v:SetMoveType( MOVETYPE_NOCLIP )
noclip:logstring ("#1s gave noclip to #2s" )

if v:GetMoveType() == MOVETYPE_NOCLIP then
v:SetMoveType( MOVETYPE_WALK )
noclip:oppositeLogString( "#1s Revoked noclip from #2s" )
end

I just placed this in the fun.lua

This is were i placed it:

Code: [Select]
local CATEGORY_NAME = "Fun"

------------------------------ Slap ------------------------------
function ulx.slap( calling_ply, target_plys, dmg )
ulx.callForEachPly( ULib.slap, target_plys, dmg )
return true
end

local slap = ulx.command( CATEGORY_NAME, "ulx slap", ulx.slap, "!slap" )
slap:addParam{ type=ULib.cmds.PlayersArg }
slap:addParam{ type=ULib.cmds.NumArg, min=0, default=0, hint="damage", ULib.cmds.optional, ULib.cmds.round }
slap:defaultAccess( ULib.ACCESS_ADMIN )
slap:help( "Slaps target(s) with given damage." )
slap:logString( "#1s slapped #2s with #3i damage" )
ulx.addToMenu( ulx.ID_MCLIENT, "Slap", "ulx slap" )


------------------------------Noclip -----------------------------
if v:GetMoveType() == MOVETYPE_WALK then
v:SetMoveType( MOVETYPE_NOCLIP )
noclip:logstring ("#1s gave noclip to #2s" )

if v:GetMoveType() == MOVETYPE_NOCLIP then
v:SetMoveType( MOVETYPE_WALK )
noclip:oppositeLogString( "#1s Revoked noclip from #2s" )
end

------------------------------ Whip ------------------------------

Help? :)

This is my 2 first lua edits/creates.
So a explaination of edits done, would  be greatful! :)
« Last Edit: August 20, 2010, 08:34:14 PM by Stickly Man! »

Offline slowtech

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Need some help with lua! :)
« Reply #1 on: August 20, 2010, 07:21:07 PM »
Also this:

Code: [Select]
function AdminJoin( ply)

         if ( ply:IsAdmin() ) then return end
                             
                             msg "An administrator has joined the game"

         end
 hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", AdminJoin );
 
if ( ply:IsSuperAdmin() ) then return end

msg "An Super Administrator has joined the game"

end

I know something is wrong with it, and/or where should it be placed?

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: Need some help with lua! :)
« Reply #2 on: August 20, 2010, 08:32:53 PM »
Didn't take too close of a look at the first one, but I will help you with the second one!  :P

Starting with your hook.Add function, it needs 3 parameters: Hook name, Unique name, and function. You have the hookname and the function right, however, you should probably use a unique name that's a little more.. unique. (Just to be safe. Any unique name you want will work, as long as someone else doesn't have a hook with the EXACT same unique name.)
Code: [Select]
hook.Add( "PlayerInitialSpawn", "msgAdminonSpawn", AdminJoin )(Also, you don't need any semicolons ';' in lua code to end a line :P)


Next, let's look at your "AdminJoin" function. All functions should have a start and an end, and when the function is called, all of the code inbetween is executed. You put your hook.Add function INSIDE your AdminJoin function, so, you would have to call your AdminJoin function to create the hook-- which is not what you want. Here's how it should be:
Code: [Select]
local function AdminJoin( ply )

end
hook.Add( "PlayerInitialSpawn", "msgAdminonSpawn", AdminJoin )
(You should add "local" before a function like this, since the only place that you call AdminJoin is on the hook.Add line right next to it. If you call it in multiple places or in different lua files, you should remove the local).


Now for the "return" statement. Executing "return" will end the function, and should really only be called if you need the function to end prematurely. You can also send a value with return, but you won't be doing that here. So when you have:
Code: [Select]
if ( ply:IsAdmin() ) then return endYou're saying: If the player is an admin, then exit the function. That's obviously not what you want. In a function like this, you won't be needing a return statement at all.


If statements are like functions-- they too have an 'end' statement. Most if statements go like this:
Code: [Select]
if <condition> then
   <your code>
end
In your case, condition would be ply:IsAdmin(), and the code you want run when that condition is true would be the message display. Another thing you can do with if statements is run code if the condition is NOT true, like so:
Code: [Select]
if 5 > 7 then
   print( "Five is greater than seven! You have some serious issues!" )
else
   print( "Five is less than seven." )
end
(Conditions don't need to be in parentheses if you don't want to).


You can also piggyback if statements for multiple conditions, which I'll show you with your own code:
Code: [Select]
local function AdminJoin( ply )
   if ply:IsAdmin() then
      msg( "An administrator has joined the game!" )
   elseif ply:IsSuperAdmin() then
      msg( "A super administrator has joined the game!" )
   end
end
hook.Add( "PlayerInitialSpawn", "msgAdminonSpawn", AdminJoin )
(Using elseif as one word is shorthand for doing else, followed by another if statement)
Also, you should note that lua IS case-sensitive, and since Msg is a function, the parameter needs to be in parentheses, so the proper command for displaying a message is: Msg( "string" )


Next thing to note is your use of Msg() --If you look at the gmod wiki on the PlayerInitialSpawn Hook, you'll notice that it's a SERVER hook. This means this code needs to be running on the server. Msg(), however, will display a message in the CONSOLE of wherever the code is running, in this case, the server console. So, if you want to display this on the chat of all of the players, then I would recommend you use the ULib.tsay() function like so:
Code: [Select]
ULib.tsay( nil, "An administrator has joined the game" )(The nil is supposed to be the player to send the message to, but ULib says that setting it to nil will send it to all players)


Also, since you're using ULX/ULib, instead of using Garry's Player:IsAdmin() and Player:IsSuperAdmin(), you should use the Player:GetUserGroup() function ULib provides, (As you can specify more groups than just Admin and Superadmin. Thus bringing your final code to look something like this:
Code: [Select]
local function AdminJoin( ply )
   if ply:GetUserGroup() == "admin" then
      ULib.tsay( nil, "An administrator has joined the game" )
   elseif ply:GetUserGroup() == "superadmin" then
      ULib.tsay( nil, "A super administrator has joined the game" )
   end
end
hook.Add( "PlayerInitialSpawn", "msgAdminonSpawn", AdminJoin )
(Also, you can use ply:GetNick() to get the name of the player that's joining, if you want to send that out with your message!)


Now save that in a lua file somewhere on your server (garrysmod\lua\autorun\), and see if it works! (Didn't test it, but I'm 90% positive it should :P)

Man, that took me longer than I anticipated. And ended up being longer than I thought it would, too :o Hope that helps you out!
I should get back to workin' on XGUI  ;D
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline slowtech

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Need some help with lua! :)
« Reply #3 on: August 21, 2010, 09:34:19 AM »
Wow stickly.
I never thought i would get a so good answer.

As well i would i knew i would have an answer, just not so long! :p

What is XGUI? ;)

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: Need some help with lua! :)
« Reply #4 on: August 21, 2010, 09:59:00 AM »
Tis' a "must-have" GUI for the SVN versions of ULX/ULib! ;D It's going to replace the old ulx menus pretty soon, so you should check it out!

http://forums.ulyssesmod.net/index.php/topic,4080.0.html
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need some help with lua! :)
« Reply #5 on: August 21, 2010, 12:21:57 PM »
Shameless.
For every 5 posts Stickly Man makes, 4 of them end up involving XGUI.
Now mind you, it doesn't bother me. We've probably been needing a new GUI since about version 1.something of ULX. Or at least easier way to modify it/add menus. Anyone of us have considered taking on the project and conveniently let other things get in the way.

I just think it's fun. And, shameless.
:P
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming