• Checking for Player Weapons
    4 replies, posted
I want a function that checks if a player is holding an actual weapon that has ammo, if not then I want to return false. Here's what I have so far but it's not working. [CODE] function PlayerHoldingWep( ply ) local nope = { "weapon_physcannon", "weapon_physgun", "weapon_crowbar", "gmod_tool", "gmod_camera" } for k, v in pairs( nope ) do if ply:GetActiveWeapon() == v then return false elseif wep:IsCarriedByLocalPlayer() then return true end end end [/CODE]
So for most weapons we can use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Weapon/GetMaxClip1]Weapon:GetMaxClip1[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Weapon/GetMaxClip2]Weapon:GetMaxClip2[/url] to check a weapon's maximum clips. With most non-ammo engine weapons like the physgun or crowbar, this will return -1. But with bugbait, this returns 0. With most Lua weapons, you could alternatively check the weapon's Primary/Secondary table. But the above will likely work fine. Some modders however may choose to make their weapon differently and not use the base ammo system, and in those cases you'd have to look how the weapon works. In most cases this should work (untested) [code] function PlayersWeaponUsesAmmo( ply ) local activeWep = ply:GetActiveWeapon() if IsValid(activeWep) and ( (activeWep.GetMaxClip1 and activeWep:GetMaxClip1() > 0) or (activeWep.GetMaxClip2 and activeWep:GetMaxClip2() > 0) ) then return true end return false end [/code] This first checks they have a valid weapon, then checks that the function exists, then finally checks if it has a maximum clip greater than 0. [b]Edit:[/b] I may have read your post wrong. If you want to check if the weapon currently has any ammo inside of it, replace my GetMaxClip1 and GetMaxClip2 with just [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Weapon/Clip1]Clip1[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Weapon/Clip2]Clip2[/url] I read your post as "How do I check if the player has a weapon that uses ammo" but you put "has ammo" so you'd use those two to check the current ammo.
[QUOTE=jackool;50354597]Useful things[/QUOTE] Now that I look at this I worded it pretty badly. I want to check if the player has a weapon that can fire bullets or that uses ammo & then proceed to check if the player has any of those weapons equiped.
[QUOTE=MexicanR;50354702]Now that I look at this I worded it pretty badly. I want to check if the player has a weapon that can fire bullets or that uses ammo & then proceed to check if the player has any of those weapons out.[/QUOTE] Alright let's take the function I provided and change it a bit. This may look complicated but it's mainly because I split everything up. Instead of what I did above, we'll make it a function that checks if a provided weapon uses ammo: [code] function DoesWeaponUseAmmo( wep ) -- Check if the weapon is valid, and if so check if it uses bullets from it's max clip if IsValid(wep) and ( (wep.GetMaxClip1 and wep:GetMaxClip1() > 0) or (wep.GetMaxClip2 and wep:GetMaxClip2() > 0) ) then return true end return false end [/code] Then for this part... [QUOTE=MexicanR;50354702]I want to check if the player has a weapon that can fire bullets[/QUOTE] We can use that function to do this: [code] function PlayerBulletWeapons( ply ) local bulletWeps = {} -- The table we will be returning -- Loop through the player's weapons for _, wep in pairs( ply:GetWeapons() ) do -- Check if this weapon uses ammo if DoesWeaponUseAmmo( wep ) then -- It does, add it to the table we are going to return table.insert( bulletWeps, wep ) end end -- Return that table! return bulletWeps end [/code] Which goes through the player's weapons and returns a table of their weapons that use ammo. Alright, so... [QUOTE=MexicanR;50354702]then proceed to check if the player has any of those weapons out.[/QUOTE] We can then put it all together and do this: [code] function PlayerUsingBulletWeapon( ply ) -- Check for a valid active weapon, then check if that active weapon is in the player's table of bullet based weapons local activeWep = ply:GetActiveWeapon() return IsValid(activeWep) and table.HasValue( PlayerBulletWeapons( ply ), activeWep ) end [/code] Which checks that they have a valid weapon out, then checks if that weapon is in their table of bullet based weapons. If you ~only~ need to check if their current weapon uses bullets, use the function in my first post because it will be faster. Hopefully that makes sense. It's pretty split apart just so it's a bit easier to understand. Untested but should work fine unless I typo'd something.
[QUOTE=jackool;50354826]Stuff[/QUOTE] Thanks!
Sorry, you need to Log In to post a reply to this thread.