If I can nitpick for a moment, the elseif statement here is redundant. A simple "else" would suffice, since you're checking for what is defined as a binary condition (sbox_godmode:GetBool() is either true or it isn't).
Yeah, that's true. I guess I wasn't thinking.
After adding it to my server it looks like it will work, I havent tried it on someone but heres the final code.
I'm not sure what you want, but I'll just give you a few general comments. Your code is very garbled. For starters, it isn't a good idea to use unnamed function declaration for a 40 line function. You can move your entire "AcceptDuel" function outside of the ulx.duel function, and then just reference it in the hook.Add.
local function acceptDuel( ply )
--code
end
function ulx.duel( calling_ply, target_ply )
hook.Add( "AcceptCheck", "AcceptDuel", acceptDuel( target_ply ) )
end
On line 1 of the "AcceptDuel" function you are referencing 'text', a variable not declared in your code. Unless this is defined somewhere else in your file and you aren't showing us it, this will throw an error.
Next, on line 2 you have an if statement that contains the rest of the function. To make it easier to read, you can just make an if statement to check for the opposite condition and return if it is true.
if not text == "!accept" then return end
This removes an entire layer of indentation from your code, and removes one of the eight end statements you have at the end.
Again, on line 4 you have another if statement that contains the rest of the function. You can use the previously mentioned technique of simply returning if it is false, or you can combine it into the previous if statement like so:
if not text == "!accept" or not sbox_godmode:GetBool() then return end
For this to work you would have to move the declaration statement of sbox_godmode above the if statement.
Then on line 8 you are declaring a function inside a function. This is messy, and introduces unnecessary layers of indentation. Instead, you can declare a function outside and then do this:
function GM:PlayerDeath = playerDeath( victim, inflictor, attacker )
Of course, you would need to change playerDeath to whatever you named the outside function.
Now in the playerDeath function, on lines 9 and 10 you reference the variable 'text' which is still not declared.
For some reason in this function you have an if statement like this:
if ( victim == attacker ) then
--code
elseif not sbox_godemode:GetBool() then
--code
end
I'm not sure if this was intentional, or if you meant to have the elseif to continue from your initial if loop (the one that surrounded all of the code). If it was intentional, then you need to declare sbox_godmode (I assume godemode was an accidental spelling error) again (because we've moved it to it's own function). If you want, you could just declare sbox_godmode outside of any functions thus allowing both functions to access it.
In this if statement you also reference text, which is still not declared.
After all of these changes, I got this:
local CATEGORY_NAME = "Advanced Simplicity"
sbox_godmode = GetConVar( "sbox_godmode" )
function playerDeath( victim, inflictor, attacker )
if ( victim == attacker ) then -- Print death
PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
game.ConsoleCommand( "sbox_godmode 1" )
PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
if ply:IsUserGroup( "superadmin" ) then
hook.Add( "SGodmode", "SwitchGod", function( command )
text = string.lower( text ) --text isn't defined!
if ( text == "!sgodmode" ) then --still not defined
game.ConsoleCommand( "sbox_godmode 0" )
end
end )
end
elseif not sbox_godemode:GetBool() then
game.ConsoleCommand( "sbox_godmode 0" ) -- Line 10.
PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) -- Print death
PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
game.ConsoleCommand( "sbox_godmode 1" )
PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
if ply:IsUserGroup( "superadmin" ) then
hook.Add( "SGodmode", "SwitchGod", function( command )
text = string.lower( text ) --text isn't defined!
if ( text == "!sgodmode" ) then --still not defined
game.ConsoleCommand( "sbox_godmode 0" )
end
end )
end
end
end
local function acceptDuel( ply )
text = string.lower( text ) --text isn't defined!
if not text == "!accept" or not sbox_godmode:GetBool() then return end --still not defined
game.ConsoleCommand( "sbox_godmode 0" ) -- Back up on 1/0. Does not print value, have to make sure either way
game.ConsoleCommand( "ulx god *" ) -- Other player protection
game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply ) -- Ungod the players so they can fight
function GM:PlayerDeath = playerDeath( victim, inflictor, attacker )
end
function ulx.duel( calling_ply, target_ply )
ply:ChatPrint( calling_ply "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply )
hook.Add( "AcceptCheck", "AcceptDuel", acceptDuel( target_ply ) )
end
local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" )
duel:defaultAccess( ULib.ACCESS_ALL )
duel:help( "Challenge a friend to a duel. !duel NAME." )
The text value isn't declared, so that will give you errors but other than that I don't think there are any actual lua errors. Whether or not the code actually does what you want, that is another story.