How to make a simple autoshoot
From GMod Wiki
m (Added tabbing, grammar correction) |
|||
Line 26: | Line 26: | ||
hook.Add( "Think", "TriggerBot", function() | hook.Add( "Think", "TriggerBot", function() | ||
- | local Target = LocalPlayer():GetEyeTrace().Entity // Now we have the entity the player is aiming at. | + | 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 | + | |
end ) | end ) | ||
Line 40: | Line 41: | ||
hook.Add( "Think", "TriggerBot", function() | hook.Add( "Think", "TriggerBot", function() | ||
- | local Target = LocalPlayer():GetEyeTrace().Entity | + | 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 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 | + | if !Firing then // If we aren't firing |
- | RunConsoleCommand( "+attack" ) // Then fire | + | RunConsoleCommand( "+attack" ) // Then fire |
- | LocalPlayer():GetActiveWeapon().SetNextPrimaryFire( LocalPlayer():GetActiveWeapon() ) // We need it to stop and start shooting, luckily weapons have delay already coded. | + | 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 | + | Firing = true // Now we are firing |
- | else // If we are already firing | + | else // If we are already firing |
- | RunConsoleCommand( "-attack" ) // Stop firing | + | RunConsoleCommand( "-attack" ) // Stop firing |
- | Firing = false // We're not anymore firing | + | Firing = false // We're not anymore firing |
- | end | + | end |
- | end | + | end |
end ) | end ) | ||
Line 75: | Line 76: | ||
hook.Add( "Think", "Triggerbot", function() | hook.Add( "Think", "Triggerbot", function() | ||
- | local Target = LocalPlayer():GetEyeTrace().Entity | + | local Target = LocalPlayer():GetEyeTrace().Entity |
- | if TriggerBot:GetInt() == 1 and LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon():IsValid() and ( Target:IsPlayer() or Target:IsNPC() ) then | + | if TriggerBot:GetInt() == 1 and LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon():IsValid() and ( Target:IsPlayer() or Target:IsNPC() ) then |
- | if !Firing then | + | if !Firing then |
- | RunConsoleCommand( "+attack" ) | + | RunConsoleCommand( "+attack" ) |
- | LocalPlayer():GetActiveWeapon().SetNextPrimaryFire( LocalPlayer():GetActiveWeapon() ) | + | LocalPlayer():GetActiveWeapon().SetNextPrimaryFire( LocalPlayer():GetActiveWeapon() ) |
- | Firing = true | + | Firing = true |
- | else | + | else |
- | RunConsoleCommand( "-attack" ) | + | RunConsoleCommand( "-attack" ) |
- | Firing = false | + | Firing = false |
- | end | + | end |
- | end | + | end |
end ) | end ) |
Latest revision as of 14:40, 28 September 2011
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.