Garry's Mod Wiki

Revision Difference

math.Clamp#526855

<function name="Clamp" parent="math" type="libraryfunc"> <description>Clamps a number between a minimum and maximum value</description> <realm>Shared and Menu</realm> <file line="39-L47">lua/includes/extensions/math.lua</file> <file line="39-L41">lua/includes/extensions/math.lua</file> <args> <arg name="input" type="number">The number to clamp.</arg> <arg name="min" type="number">The minimum value, this function will never return a number less than this.</arg> <arg name="max" type="number">The maximum value, this function will never return a number greater than this.</arg> </args> <rets> <ret name="" type="number">The clamped value.</ret> </rets> </function> <example> <description>Demonstrates what this function does.</description> <code> print( math.Clamp( 10, 0, 5 ) ) -- 10 is greater than 5 so returns 5 print( math.Clamp( 3, 0, 5 ) ) -- 3 is greater than 0 and less than 5, so returns 3 print( math.Clamp( -1, 0, 5 ) ) -- -1 is less than 0, so returns 0 </code> <output> 5 3 0 </output> </example> <example> <description>Heals player "ply" by 10 health, but won't let their health go above 100.</description> <code>ply:SetHealth( math.Clamp( ply:Health() + 10, 0, 100 ) )</code> </example>