• SWEP disallow interacting with things
    17 replies, posted
Hello. I have the quick question of: How would I make a SWEP that when equipped you can't interact with ANYTHING(Pressing E to use buttons, etc.)
Use the following hook [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerUse]GM:PlayerUse[/url] And the following function: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/GetActiveWeapon]Player:GetActiveWeapon[/url]
[QUOTE=gonzalolog;52731470]Use the following hook [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerUse]GM:PlayerUse[/url] And the following function: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/GetActiveWeapon]Player:GetActiveWeapon[/url][/QUOTE] not sure what further to do but thanks I would personally think putting hook.Add( "PlayerUse", "DoStuff", function( ply, ent ) return false end ) would stop it from happening, but can you even put hooks in a SWEP file?
[QUOTE=jonnyetiz;52731561]not sure what further to do but thanks I would personally think putting hook.Add( "PlayerUse", "DoStuff", function( ply, ent ) return false end ) would stop it from happening, but can you even put hooks in a SWEP file?[/QUOTE] You can put hooks in any .lua file.
[QUOTE=jonnyetiz;52731561]not sure what further to do but thanks I would personally think putting [code]hook.Add( "PlayerUse", "DoStuff", function( ply, ent ) return false end )[/code] would stop it from happening, but can you even put hooks in a SWEP file?[/QUOTE] Using that code will prevent ALL players from being able to interact with anything. You can however use the hook to get what you want, by checking if the players active weapon (function provided by gonzalolog) has the same class as the weapon that you want to block (if the weapon is in the folder/file called weapon_block, then its class will be weapon_block.) To get the active weapon's class, use the Entity:GetClass function
What's going on with the waves of requesters that just want us to code for them
could I potentially do something along the lines of: [code] if (ply:GetActiveWeapon() == "weapon_name") then hook.Add( "PlayerUse", "DoStuff", function( ply, ent ) return false end ) end [/code]
[url]http://wiki.garrysmod.com/page/Hook_Library_Usage[/url]
[CODE] hook.Add( "PlayerUse", "DoThings", function( ply ) if ( ply:GetActiveWeapon() == "weapon_name" ) then return false else return true end end) [/code]
[QUOTE=jonnyetiz;52732606][CODE] hook.Add( "PlayerUse", "DoThings", function( ply ) if ( ply:GetActiveWeapon() == "weapon_name" ) then return false else return true end end) [/code][/QUOTE] Remove the else and then you've got it.
[QUOTE=txike;52732612]Remove the else and then you've got it.[/QUOTE] Thanks [editline]30th September 2017[/editline] No matter what I've tried with the hook, I have had no luck. I have put it in a sv file, the swep file but when the player has the weapon I have specified active, they can still interact.
[CODE] if SERVER then hook.Add( "PlayerUse", "MySWEPPreventInput", function( ply ) if ply:GetActiveWeapon( ) == "my_shitty_weapon" then return false end end ) end [/CODE] Replace my_shitty_weapon with whatever your shitty weapon's class name is. You also can change the hook identifier if you want. I could not use tabs for whatever reason, so I had to use spaces to indent. :hammered: [editline]30th September 2017[/editline] Also, it would be nice if you were to provide your code, so we can help you better.
[QUOTE=brianm109;52732938][CODE] if SERVER then hook.Add( "PlayerUse", "MySWEPPreventInput", function( ply ) if ply:GetActiveWeapon( ) == "my_shitty_weapon" then return false end end ) end [/CODE] Replace my_shitty_weapon with whatever your shitty weapon's class name is. You also can change the hook identifier if you want. I could not use tabs for whatever reason, so I had to use spaces to indent. :hammered: [editline]30th September 2017[/editline] Also, it would be nice if you were to provide your code, so we can help you better.[/QUOTE] Should i put this in the SWEP or a seperate sv_init.lua or something like that
can just do return not ply:GetActiveWeapon() == "my_weapon" instead of all the if else shit
Didnt work.
It's because it's ply:GetActiveWeapon():GetClass() ply:GetActiveWeapon() returns the entity itself.
Sorry, you need to Log In to post a reply to this thread.