How to make a simple autoshoot
From GMod Wiki
Lua: How to make a simple autoshoot |
Description: | Article on how to make a simple autoshoot. |
Original Author: | Assault_Trooper |
Created: | October 24th 2010 |
Introduction
I will teach you in this article on how to make a simple autoshoot, or known by other name triggerbot.
Coding the triggerbot
First, you will have to create the hook for it.
hook.Add( "Think", "TriggerBot", function() end )
Now we have the hook for it, but I think we'll need something more also. Lets start by getting the entity, that the player is aiming at.
hook.Add( "Think", "TriggerBot", function() local Target = LocalPlayer():GetEyeTrace().Entity // Now we have the entity the player is aiming at. // Check if the players weapon is valid and is he alive. if LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon():IsValid() then // This part would be running everytime you aim at a valid entity. end end )
As you can see, we're now getting that the players aimed entity is valid, and our weapon is valid. Now we must check is the target a player or a npc, we dont want to keep shooting on every damn prop do we?
hook.Add( "Think", "TriggerBot", function() local Target = LocalPlayer():GetEyeTrace().Entity if LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon():IsValid() and ( Target:IsPlayer() or Target:IsNPC() ) then // This is an important part, now we're checking is the target a player or a npc. if !Firing then // If we aren't firing RunConsoleCommand( "+attack" ) // Then fire LocalPlayer():GetActiveWeapon().SetNextPrimaryFire( LocalPlayer():GetActiveWeapon() ) // We need it to stop and start shooting, luckily weapons have delay already coded. Firing = true // Now we are firing else // If we are already firing RunConsoleCommand( "-attack" ) // Stop firing Firing = false // We're not anymore firing end end end )
Optional functions
You might want to toggle the autoshoot, I'll teach you how to do it. First, create a ConVar.
local TriggerBot = CreateClientConVar( "triggerbot_enabled", 0, true, false )
Add it to the top like shown below. And then make the switch on the entity check.
local TriggerBot = CreateClientConVar( "triggerbot_enabled", 0, true, false ) hook.Add( "Think", "Triggerbot", function() local Target = LocalPlayer():GetEyeTrace().Entity if TriggerBot:GetInt() == 1 and LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon():IsValid() and ( Target:IsPlayer() or Target:IsNPC() ) then if !Firing then RunConsoleCommand( "+attack" ) LocalPlayer():GetActiveWeapon().SetNextPrimaryFire( LocalPlayer():GetActiveWeapon() ) Firing = true else RunConsoleCommand( "-attack" ) Firing = false end end end )
And now you have an toggleable triggerbot!
And here ends this article, hope you learned something about triggerbots.