Need some help with small script

Hi everyone! I just wrote script that will strip weapon with class “khr_m82a3” if you will get it, but it is broken. What should i do?



local function restrict()
	if ply:GetActiveWeapon():GetClass() == "khr_m82a3" then
		ply:StripWeapon("khr_m82a3")
	end
end

hook.Add( "Think", "Weapon restrict", restrict )


The path to script is /addons/weapon_restrict/autorun/sh_restrict.lua. I also tried “cl_” and “sv_”.
Help please!

ply is nil, define it. Don’t use think here, either use:

GM:PlayerCanPickupWeapon or

GM:PlayerSwitchWeapon

How? I’m noob in writing scripts from scratch.

UPD:
Now my script looks like this:



local playerCurrent = Entity( 1 )

local function restrict(ply)
	if ply:GetActiveWeapon():GetClass() == "khr_m82a3" then
		ply:StripWeapon("khr_m82a3")
	end
end

hook.Add( "PlayerSwitchWeapon", "Weapon blacklist", restrict(playerCurrent) )


But doesn’t work.

Remove playerCurrent, and replace restrict(playerCurrent) with restrict



hook.Add( "PlayerCanPickupWeapon", "BlockKHRm82a3", function( ply, wep )
	if wep:GetClass() == "khr_m82a3" then -- if the weapon they are trying to pick up is a KHR m82a3
		wep:Remove() -- remove the one they were trying to pick up
		return false -- don't give them a khr_m82a3
	end
end )


Just modified an example in: http://wiki.garrysmod.com/page/GM/PlayerCanPickupWeapon

He clearly just want’s to strip a weapon that someone has, and the code he gave us should work if he used the Player Valuables right.

Using LocalPlayer() in a client folder should work like 100x better. If not check option 2.


local function restrict()
	if LocalPlayer():GetActiveWeapon():GetClass() == "khr_m82a3" then
		LocalPlayer():StripWeapon("khr_m82a3")
	end
end

hook.Add( "Think", "Weapon restrict", restrict )


 local meta = FindMetaTable( "Player" )
local function restrict()
	if meta:GetActiveWeapon():GetClass() == "khr_m82a3" then
		meta:StripWeapon("khr_m82a3")
	end
end

hook.Add( "Think", "Weapon restrict", restrict )

Both of the examples you’ve provided won’t work. First, StripWeapon is only available serverside. Second, that’s not how you use meta tables. Third, there’s absolutely no need to check the player’s current weapon EVERY FRAME.

Like Invule mentioned above,

GM:PlayerSwitchWeapon and

GM:PlayerCanPickupWeapon exist. I’d recommend using PlayerSwitchWeapon if the players aren’t just getting the weapon from picking it up off the ground. Here’s an example for you:
[lua]
local function restrict( ply, oldwep, newwep ) – hook arguments
if newwep:GetClass() == “khr_m82a3” then – check if the weapon being switched to is the blacklisted one
ply:StripWeapon(“khr_m82a3”) – strip the weapon
return true – returning true prevents switching to the weapon
end
end

hook.Add( “PlayerSwitchWeapon”, “Weapon restrict”, restrict )
[/lua]
Make sure to add an “if SERVER then” check above your code since we only need this to be ran serverside and you’ve currently got the code shared. Alternatively, create a server folder in your autorun folder and drop the file in there.

Doesn’t work. Both variants.

If you can provide the error you’re receiving and the code you’re trying to run, that would be helpful.

There is no errors in console, i’m trying to run your code that you wrote above.

Don’t use scratch. Or do you mean scratch as in you are writing from nothing?

Writing from nothing ofcourse.

Did you look at wiki?

PlayerCanPickUpWeapon

After testing, it works perfectly fine for me. Please show your entire file and where you’ve got it located.

[editline]1st December 2016[/editline]

You could also just be using an incorrect weapon class.

As bosnian_cyco pointed out, you should use PlayerCanPickupWeapon. (untested code, only serverside)
[lua]local BannedWepList = {
[“khr_m82a3”] = true
}

hook.Add(“PlayerCanPickupWeapon”, “Restrict_BannedWepList”, function(ply, wep)
if BannedWepList[wep:GetClass()] then
ply:PrintMessage(HUD_PRINTTALK, "You can not pickup weapon " … wep:GetClass())

    return false
end

end)[/lua]
You could take the code a step further and make a check for the player’s rank. (also untested)
[lua]local BannedWepList = {
allowed_ranks = {
[“admin”] = true
},
weapon_blacklist = {
[“khr_m82a3”] = true
}
}

hook.Add(“PlayerCanPickupWeapon”, “Restrict_BannedWepList”, function(ply, wep)
if BannedWepList.weapon_blacklist[wep:GetClass()] and not BannedWepList.allowed_ranks[ply:GetUserGroup()] then
ply:PrintMessage(HUD_PRINTTALK, "You can not pickup weapon " … wep:GetClass())

    return false
end

end)[/lua]

For a code walkthrough
[lua]local BannedWepList = { – Create a localized table with the name BannedWepList
[“khr_m82a3”] = true – Add the weapon class of “khr_m82a3” to the table and set the value to true
} – End of localized table “BannedWepList”

hook.Add(“PlayerCanPickupWeapon”, “Restrict_BannedWepList”, function(ply, wep) – Create the hook with the function. PlayerCanPickupWeapon seems like more of a logical choice.
if BannedWepList[wep:GetClass()] then – Check if the weapon’s class is inside of the table, if true, then continue
ply:PrintMessage(HUD_PRINTTALK, "You can not pickup weapon " … wep:GetClass()) – Give the player an error message

    return false -- Return false, returning false prevents the player from picking up the weapon
end -- End of the if statement

end) – End of the hook[/lua]

Seems like this code is crashing my server, lol.

The :GetClass() won’t return the entity name, it just returns class.
Print out the weapon table with k,v and find the path for the entity name.

I don’t see why it would be causing crashes. I’ll look further into it when I get home. Are there any script errors? Are you only running it server-side? Place it in lua/autorun/server or place it in lua/autorun and add
[lua]if SERVER then[/lua]
At the beginning and don’t forget to add an end on the last line of the file. That will end the if statement for the server check.

Running only serverside, path - lua/autorun/server.

https://wiki.garrysmod.com/page/Entity/GetClass it returns the entity’s class name. Look at the example. He stated he had the class name for the weapon he is wanting to restrict. khr_m82a3

[editline]3rd December 2016[/editline]

It crashes when they attempt to pickup the weapon?