ply:PrintMessage Spam

Made a simple thing to prevent from picking up heavy props but get spammed with the player message. Is there anyway to prevent the ply:PrintMessage from spamming when I try to pick it up? Here’s the code.

[lua] function TooHeavy( ply, ent )
if ent:GetPhysicsObject():GetMass() >= 75 then
ply:PrintMessage(HUD_PRINTTALK, “You can’t pick this up! It’s too heavy”)
return false
else
return true
end
end
hook.Add( “PhysgunPickup”, “TooHeavy”, TooHeavy)
[/lua]

Maybe you could use Player.KeyPressed so that it only shows once per right click.

Try adding a cooldown timer.
[lua]
function TooHeavy(ply, prop)
if (prop.LastTriedPickup == nil or prop.LastTriedPickup + 5 < CurTime()) and prop:GetPhysicsObject():GetMass() >= 75 then
prop.LastTriedPickup = CurTime()
ply:PrintMessage(HUD_PRINTCENTER, “hey asshole that’s too heavy”)
end
end
hook.Add(“PhysgunPickup”, “TooHeavy”, TooHeavy)
[/lua]

Bit of a monster if line, but basically it checks to see if it was picked up in the last 5 seconds and it’s too heavy. If it is it sets the last-pickup-denied time for the prop and gives him a message.

[lua]hook.Add( “PhysgunPickup”, “TooHeavy”, function(ply, ent) – You do not need anything but an inline function for hooks.
if (not IsValid(ent)) then – We don’t want to mess with invalid ents
return – Always use return on it’s own unless you want to override the default behaviour. We don’t want to.
end
local phys = ent:GetPhysicsObject();
if (IsValid(phys) and phys:GetMass() >= 75) then – Make sure it’s a valid physics object before using it, or errors will happen.
ply._NextGrabMsg = ply._NextGrabMsg or 0; – Make sure there is a number var called ._NextGrabMsg
if (ply._NextGrabMsg < CurTime()) then – See if they can be msg’d
ply._NextGrabMsg = CurTime() + 5; – make it so they can’t be msg’d for another 5s
ply:ChatPrint(“You can’t pick htis up! It’s too heavy”); – ChatPrint is the same as PrintMessage(HUD_PRINTTALK)
end
return false; – Stop them picking it up
end
– DO NOT RETURN TRUE HERE! If you do return true, then you will override the default behaviour that stops players picking up things that they shouldn’t
end);[/lua]

Thanks a lot man