SWEP disallow interacting with things

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

GM:PlayerUse

And the following function:

Player:GetActiveWeapon

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?

You can put hooks in any .lua file.

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:



if (ply:GetActiveWeapon() == "weapon_name") then
     hook.Add( "PlayerUse", "DoStuff", function( ply, ent )
	return false
     end )
end




hook.Add( "PlayerUse", "DoThings", function( ply )
     if ( ply:GetActiveWeapon() == "weapon_name" ) then
	return false
     else
	return true
     end
end)


Remove the else and then you’ve got it.

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.



if SERVER then
    hook.Add( "PlayerUse", "MySWEPPreventInput", function( ply )
        if ply:GetActiveWeapon( ) == "my_shitty_weapon" then
            return false
        end
    end )
end


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.

Should i put this in the SWEP or a seperate sv_init.lua or something like that

Put it wherever you want, in any script that’s executed serverside.

can just do return not ply:GetActiveWeapon() == “my_weapon” instead of all the if else shit

No, because that way you’re returning the hook in any scenario, and that’s bad because it prevents any other hooks from running after this one.

Didnt work.

It’s because it’s ply:GetActiveWeapon():GetClass()
ply:GetActiveWeapon() returns the entity itself.