• How do I make this toggle-able?
    21 replies, posted
[h=2][/h]Hello, So, I got a script from Gravious, which basically makes a little box appear to the bottom right side of your screen showing the key(s) pressed and a number of times you've strafed within a certain period. What I would like to do is to toggle it from showing only if a command such as "!showkeys" is typed. I would also like to see it toggle back on via the command "!showkeys" but I'm not too sure how to make that possible so I would appreciate if anyone could help me with this. Here is the HUD Lua script. [lua]-- HUD module used for my tutorials local StrafeAxis = 0 -- Saves the last eye angle yaw for checking mouse movementlocal StrafeButtons = nil -- Saves the buttons from SetupMove for displayinglocal StrafeCounter = 0 -- Holds the amount of strafeslocal StrafeLast = nil -- Your last strafe key for counting strafeslocal StrafeDirection = nil -- The direction of your strafes used for displayinglocal StrafeStill = 0 -- Counter to reset mouse movementlocal fb, ik, lp, ts = bit.band, input.IsKeyDown, LocalPlayer, _C.Team.Spectator -- This function is used frequently so to reduce lag...local function norm( i ) if i > 180 then i = i - 360 elseif i < -180 then i = i + 360 end return i end -- Custom function to normalize eye angleslocal StrafeData -- Your Sync value is stored herelocal KeyADown, KeyDDown -- For displaying on the HUDlocal MouseLeft, MouseRight --- For displaying on the HUDlocal ViewGUI = CreateClientConVar( "sl_showgui", "1", true, false ) -- GUI visibilitysurface.CreateFont( "HUDFont2", { size = 20, weight = 800, font = "Tahoma" } )function ResetStrafes() StrafeCounter = 0 end -- Resets your stafes (global)function SetSyncData( data ) StrafeData = data end -- Sets your sync data (global)-- Monitors the buttons and angleslocal function MonitorInput( ply, data ) StrafeButtons = data:GetButtons() local ang = data:GetAngles().y local difference = norm( ang - StrafeAxis ) if difference > 0 then StrafeDirection = -1 StrafeStill = 0 elseif difference < 0 then StrafeDirection = 1 StrafeStill = 0 else if StrafeStill > 20 then StrafeDirection = nil end StrafeStill = StrafeStill + 1 end StrafeAxis = angendhook.Add( "SetupMove", "MonitorInput", MonitorInput )-- Monitors your key presses for strafe countinglocal function StrafeKeyPress( ply, key ) if ply:IsOnGround() then return end local SetLast = true if key == IN_MOVELEFT or key == IN_MOVERIGHT then if StrafeLast != key then StrafeCounter = StrafeCounter + 1 end else SetLast = false end if SetLast then StrafeLast = key endendhook.Add( "KeyPress", "StrafeKeys", StrafeKeyPress )-- Paints the actual HUDlocal function HUDPaintB() if not ViewGUI:GetBool() then return end if not IsValid( lp() ) or lp():Team() == ts then return end -- Background local x,y = ScrW() - 250, ScrH() - 145 surface.SetDrawColor( Color(35, 35, 35, 255) ) surface.DrawRect( x, y, 230, 125 ) -- Setting the key colors if StrafeButtons then if fb( StrafeButtons, IN_MOVELEFT ) > 0 then KeyADown = Color( 142, 42, 42, 255 ) else KeyADown = nil end if fb( StrafeButtons, IN_MOVERIGHT ) > 0 then KeyDDown = Color( 142, 42, 42, 255 ) else KeyDDown = nil end end -- Getting the direction for the mouse if StrafeDirection then if StrafeDirection > 0 then MouseLeft, MouseRight = nil, Color( 142, 42, 42, 255 ) elseif StrafeDirection < 0 then MouseLeft, MouseRight = Color( 142, 42, 42, 255 ), nil else MouseLeft, MouseRight = nil, nil end else MouseLeft, MouseRight = nil, nil end -- Box on top surface.SetDrawColor( Color(42, 42, 42, 255) ) surface.DrawRect( x + 5, y + 5, 220, 55 ) draw.SimpleText( "Extra keys:", "HUDTimer", x + 12, y + 20, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER ) -- If we have buttons, display them if StrafeButtons then local zx = x + 40 if fb( StrafeButtons, IN_FORWARD ) > 0 then draw.SimpleText( "W", "HUDTimer", zx + 56, y + 20, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER ) end if fb( StrafeButtons, IN_BACK ) > 0 then draw.SimpleText( "S", "HUDTimer", zx + 76, y + 20, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER ) end if ik( KEY_SPACE ) or fb( StrafeButtons, IN_JUMP ) > 0 then draw.SimpleText( "Jump", "HUDTimer", zx + 92, y + 20, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER ) end if fb( StrafeButtons, IN_DUCK ) > 0 then draw.SimpleText( "Duck", "HUDTimer", zx + 136, y + 20, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER ) end end -- Display the amount of strafes if StrafeCounter then draw.SimpleText( "Strafes: " .. StrafeCounter, "HUDTimer", x + 12, y + 45, Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER ) end -- If we have sync, display the sync if StrafeData then draw.SimpleText( StrafeData, "HUDTimer", x + 216, y + 45, Color(255, 255, 255), TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER ) end -- Bottom left surface.SetDrawColor( KeyADown or Color(42, 42, 42, 255) ) surface.DrawRect( x + 5, y + 65, 108, 25 ) draw.SimpleText( "A", "HUDFont", x + 58, y + 77, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER ) -- Bottom right surface.SetDrawColor( KeyDDown or Color(42, 42, 42, 255) ) surface.DrawRect( x + 118, y + 65, 107, 25 ) draw.SimpleText( "D", "HUDFont", x + 172, y + 77, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER ) -- Bottom left surface.SetDrawColor( MouseLeft or Color(42, 42, 42, 255) ) surface.DrawRect( x + 5, y + 95, 108, 25 ) draw.SimpleText( "Mouse Left", "HUDFont2", x + 58, y + 107, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER ) -- Bottom right surface.SetDrawColor( MouseRight or Color(42, 42, 42, 255) ) surface.DrawRect( x + 118, y + 95, 107, 25 ) draw.SimpleText( "Mouse Right", "HUDFont2", x + 172, y + 107, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )end hook.Add( "HUDPaint", "PaintB", HUDPaintB )[/lua] Thank you very much! [editline]19th March 2017[/editline] The script seems to be hard to read/look at so I have posted it in a pastebin: [url]http://pastebin.com/tNqhSJ7t[/url]
Something like this should work for the code you provided: [code] local txt = {"!showkeys", "/showkeys"} local function chatCommands(ply, text) if (ply:EntIndex() != LocalPlayer():EntIndex()) then return end; for k, v in pairs(txt) do if (string.lower(text) == v) then local val = GetConVar("sl_showgui"):GetInt(); if (val == 1) then val = 0; else val = 1; end RunConsoleCommand("sl_showgui", val); break; end end end hook.Add("OnPlayerChat", "doChatCommands", chatCommand); [/code] There's different ways of doing this, but this should serve you fine since the script already seems to have a convar to toggle the UI.
[QUOTE=Brassx;51985916]Something like this should work for the code you provided: [code] local txt = {"!showkeys", "/showkeys"} local function chatCommands(ply, text) if (ply:EntIndex() != LocalPlayer():EntIndex()) then return end; for k, v in pairs(txt) do if (string.lower(text) == v) then local val = GetConVar("sl_showgui"):GetInt(); if (val == 1) then val = 0; else val = 1; end RunConsoleCommand("sl_showgui", val); break; end end end hook.Add("OnPlayerChat", "doChatCommands", chatCommand); [/code] There's different ways of doing this, but this should serve you fine since the script already seems to have a convar to toggle the UI.[/QUOTE] No reason to use EntIndex when you can just compare the entities themselves. Also no reason to use semicolons in lua, especially when you seem to only sometimes use them You could also get rid of the loop [lua]local commands = { ["!showkeys"] = true, ["/showkeys"] = true, } hook.Add( "OnPlayerChat", "ToggleHUDPaintB", function( ply, text ) if ply != LocalPlayer() then return end local args = string.Split( text, " " ) if commands[args[1]] then RunConsoleCommand( "sl_showgui", math.abs( GetConVar( "sl_showgui" ):GetInt() - 1 ) ) end end )[/lua] Edited so the code is not infested with syntax errors
Huge Thanks to both of you! :) I will make sure to try out the code. :) I really appreciate the help. Also should I put this within the script or in a new lua file and if so, where? Thanks
Either in [B]garrysmod/lua/autorun/client/your_file.lua[/B] or [B]garrysmod/addons/your_folder/lua/autorun/client/your_file.lua[/B]
[QUOTE=JasonMan34;51986117]No reason to use EntIndex when you can just compare the entities themselves. Also no reason to use semicolons in lua, especially when you seem to only sometimes use them You could also get rid of the loop [lua]local commands = { ["!showkeys"] = true, ["/showkeys"] = true, } hook.Add( "OnPlayerChat", "ToggleHUDPaintB", function( ply, text ) if ply != LocalPlayer() then return end local args = string.Split( text, " " ) if commands[args[1]] then RunConsoleCommand( "sl_showgui", math.abs( GetConVar( "sl_showgui" ):GetInt() - 1 ) ) ) end end[/lua][/QUOTE] I used that code and I get: [ERROR] lua/autorun/client/showkeys.lua:11: unexpected symbol near ')' 1. unknown - lua/autorun/client/showkeys.lua:0
[QUOTE=ZombiezPvP;51992429]I used that code and I get: [ERROR] lua/autorun/client/showkeys.lua:11: unexpected symbol near ')' 1. unknown - lua/autorun/client/showkeys.lua:0[/QUOTE] Dude, it's literally just an extra ')' at the end of the line... you've gotta be able to figure [B]that[/B] out
Sorry, I didn't look at it :/ [editline]21st March 2017[/editline] Hey again, I tried to look what caused the error this time but I'm not sure about this one [ERROR] lua/autorun/client/showkeys.lua:13: ')' expected (to close '(' at line 6) near '<eof>' 1. unknown - lua/autorun/client/showkeys.lua:0
[QUOTE=ZombiezPvP;51992548]Sorry, I didn't look at it :/ [editline]21st March 2017[/editline] Hey again, I tried to look what caused the error this time but I'm not sure about this one [ERROR] lua/autorun/client/showkeys.lua:13: ')' expected (to close '(' at line 6) near '<eof>' 1. unknown - lua/autorun/client/showkeys.lua:0[/QUOTE] Learn how to read lua errors. ')' was expected to close '(' at the end of the file. i.e. - You're missing a closing bracket at the end
but all I have done is remove the extra ')'
[QUOTE=ZombiezPvP;51992775]but all I have done is remove the extra ')'[/QUOTE] You weren't suppose to remove one, you're missing one.
[QUOTE=Nick78111;51992811]You weren't suppose to remove one, you're missing one.[/QUOTE] To be fair, my original code posted had the syntax errors in it, and I edited them out. But ye, simple syntax errors are not something you should require help with...
Oh... I knew that... I thought Jason meant as it you need to remove the extra "(" not add an extra. I will try that, thank you! :) [editline]21st March 2017[/editline] It seems to not work still, is the line 11 meant to look like: [lua] RunConsoleCommand( "sl_showgui", math.abs( GetConVar( "sl_showgui" ):GetInt() - 1 ) ) ) )[/lua]
[LUA]RunConsoleCommand("sl_showgui", math.abs(GetConVar("sl_showgui"):GetInt() - 1))[/LUA]
Ok, thank you, I am also struggling on line 6 because I need to close off the bracket but where ever I close it, it gets classed as an unexpected symbol. line 6: [lua]hook.Add( "OnPlayerChat", "ToggleHUDPaintB", function( ply, text )[/lua]
[QUOTE=ZombiezPvP;51993133]Ok, thank you, I am also struggling on line 6 because I need to close off the bracket but where ever I close it, it gets classed as an unexpected symbol. line 6: [lua]hook.Add( "OnPlayerChat", "ToggleHUDPaintB", function( ply, text )[/lua][/QUOTE] You need to put a ')' at the end...
Jesus Christ just learn how to read errors/code. You've been stuck on syntax all this time. There's literally an extra ')' on the RunConsoleCommand row, anyone that knows how to read will tell you that without even knowing what coding is. And the 2nd error, which I already told you how to fix, is a missing bracket at the end of the file. [URL="https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexbfbb.html"]This might help[/URL]
[QUOTE=JasonMan34;51995059]Jesus Christ just learn how to read errors/code. You've been stuck on syntax all this time. There's literally an extra ')' on the RunConsoleCommand row, anyone that knows how to read will tell you that without even knowing what coding is. And the 2nd error, which I already told you how to fix, is a missing bracket at the end of the file. [URL="https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexbfbb.html"]This might help[/URL][/QUOTE] I've tried to put an extra '(' 3 times already but it still shows the same error. [editline]22nd March 2017[/editline] Would it require a server or a map restart to show the changes?
[QUOTE=ZombiezPvP;51996434]I've tried to put an extra '(' 3 times already but it still shows the same error. [editline]22nd March 2017[/editline] Would it require a server or a map restart to show the changes?[/QUOTE] Dude... For the THIRD time. PUT A ')' AT THE END OF THE FILE We've told you [B]exactly[/B] how to fix every single error and you managed to mess it up each time. You have to understand the frustration here
No errors but for some reason I get no reaction towards the command !showkeys or /showkeys.
Debug it
erm, I will try, thanks. :wink:
Sorry, you need to Log In to post a reply to this thread.