Didn't take too close of a look at the first one, but I will help you with the second one!
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.)
hook.Add( "PlayerInitialSpawn", "msgAdminonSpawn", AdminJoin )
(Also, you don't need any semicolons ';' in lua code to end a line
)
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:
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:
if ( ply:IsAdmin() ) then return end
You'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:
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:
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:
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:
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:
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
)
Man, that took me longer than I anticipated. And ended up being longer than I thought it would, too
Hope that helps you out!
I should get back to workin' on XGUI