[DarkRP] Send a HUD hint for players without gun license?
6 replies, posted
I'm working on a DarkRP server where players need a gun license to pick up weapons. Currently, there's no clear indicator why a player without a license can't pick up a spawned weapon, which I'd like to change.
I'm fairly new to Gmod Lua, so I have an idea of what I need to do, but I'm not sure how to execute it. I thought the following would work, but it didn't:
-- garrysmod/addons/rp_custom/lua/autorun/sh_license_notify.lua
hook.Add( "PlayerCanPickupWeapon", "NotifyGunLicense", function ( ply, weapon )
local weaponClass = weapon:GetClass()
if GAMEMODE.Config.license and not ply:getDarkRPVar("HasGunlicense") and not ply.RPLicenseSpawn then
if not GAMEMODE.NoLicense[string.lower(weaponClass)] or weapon:IsWeapon() then
if PLAYER then
notification.AddLegacy( "You need a gun license to pick this up!", NOTIFY_ERROR, 4 )
end
end
end
end )
I'm not sure what the best solution for this would be. I'd basically like a hint-style popup that says "You need a gun license to pick this up!"
Any help would be appreciated.
If you check the wiki:
GM/PlayerCanPickupWeapon is a serverside hook
and
notification.AddLegacy is a clientside function
this means that after you do the checks to verify if they can/cannot pickup a weapon you would need to network over to the CLIENT and then call notification.AddLegacy on the clientside, simply doing this in a 'shared' file would not work as the GM/PlayerCanPickupWeapon is a serverside hook.
Here is the net library reference and usage on the wiki: Net Library Usage
As I know, DarkRP already has function to send notifications
DarkRP's default implementation of GM/PlayerCanPickupWeapon already handles that for me, I just needed to hook it to send a HUD popup.
Thanks for the tip, DarkRP.notify() is what I was looking for. Here's my updated code:
hook.Add( "PlayerCanPickupWeapon", "NotifyGunLicense", function ( ply, weapon )
local weaponClass = weapon:GetClass()
if GAMEMODE.Config.license and not ply:getDarkRPVar("HasGunlicense") and not ply.RPLicenseSpawn then
if not GAMEMODE.NoLicense[string.lower(weaponClass)] or weapon:IsWeapon() then
DarkRP.notify(ply, NOTIFY_ERROR, 6, "You need a gun license to pick up that weapon!")
end
end
end )
This almost works the way I intend it to, but what I didn't realize is that PlayerCanPickupWeapon() is called repeatedly while the player is in contact with the weapon, so the notifications are spammed like this:
https://files.facepunch.com/forum/upload/172195/23a3ce32-e06e-4cfe-b39d-b85fcb480cca/image.png
How could I fix that issue?
theres probably a easier fix for that, but i would just do right now is using a boolean as check if the message has been displayed already. if it has not, display it and change the boolean to the opposite. (duh)
and also start a timer that changed the boolean back after 5 seconds or so idk.
Use can make by different hook.
GM/WeaponEquip
or
GM/PlayerSwitchWeapon
Thanks to everyone for their help! Here is my implementation that I've found to work pretty well.
hook.Add( "PlayerInitialSpawn", "SetGunLicenseNotif", function ( ply )
ply:SetNWBool( "ShouldLicenseNotify", true )
end)
hook.Add( "PlayerCanPickupWeapon", "NotifyGunLicense", function ( ply, weapon )
local weaponClass = weapon:GetClass()
local notifLength = 6
-- The first two if statements are copied from the function
-- GM:PlayerCanPickupWeapon(ply, weapon), located in
-- darkrp/gamemode/modules/base/sv_gamemode_functions.lua
if GAMEMODE.Config.license and not ply:getDarkRPVar("HasGunlicense") and not ply.RPLicenseSpawn then
if not GAMEMODE.NoLicense[string.lower(weaponClass)] or weapon:IsWeapon() then
if ply:GetNWBool( "ShouldLicenseNotify", true ) then
ply:SetNWBool( "ShouldLicenseNotify", false )
-- Push a notification to the player who wants the weapon
DarkRP.notify( ply, NOTIFY_ERROR, notifLength, "You need a gun license to pick up that weapon!" )
-- Set a timer so we don't spam ply with notifications
timer.Simple( notifLength + 2, function () ply:SetNWBool( "ShouldLicenseNotify", true ) end )
end
end
end
end )
Sorry, you need to Log In to post a reply to this thread.