So I'm attempting to create a custom item for this beautiful piece of work: [url]http://www.garrysmod.org/downloads/?a=view&id=70734[/url]
And I know nearly jack shit about Lua.
I managed to edit the rebel clothes item to make the player "wear" body armor (sets playermodel to Metrocop), and then add a "clothes" item to the player's inventory when used (and vice versa - when clothes are worn, the body armor item is added and the model is changed to male_02).
That part works flawlessly for now, so I'd like to move on to the part I've been having trouble with - the actual armor part.
I hopelessly attempted an autorun script that would check for damage and reduce it if the armor value (from the body armor) wasn't zero.
[quote=InventoryAddon/lua/autorun/takeDmg.lua][lua]function takeDmg(ply,hitgroup,dmginfo)
if(ply.armorval == .75) then
dmginfo:SubtractDamage(dmginfo:GetDamage * 0.75)
end
end
function assignArmorVal(ply,armorval)
armorval = 0
end[/lua][/quote]
Of course, I have no idea if that script is running or not (or if it would even work when it ran, for that matter).
I've coded the armor and clothes to correctly update "armorval" (at least I think I did).
[quote=InventoryAddon/lua/items/clothes.lua][lua]
-- This particular items table, must have it.
local ITEM = { }
-- The UniqueID for this item, this is used to spawn the item and it's the ID for the item. Text always has to be lowered.
ITEM.UniqueID = "clothes"
-- The model that will be shown in the inventory and on the map.
ITEM.Model = "models/Items/BoxMRounds.mdl"
-- The name for this item, used in the inventory menu.
ITEM.Name = "Clothes"
-- The description for this item, used in the inventory menu.
ITEM.Description = "A clean(?) change of clothes."
-- Remove this item from the inventory when you press use on it?
-- True will make it remove, false will make it stay.
ITEM.RemoveOnUse = true
-- The USE item function.
if ( SERVER ) then -- Only used on server so use this check.
function ITEM:USE( ply )
ply:SetModel( "models/player/Group03/male_02.mdl" )
ply:GiveItem( "metroarmor", 1 )
--ply:SetArmor (0)
ply.armorval = 0
InventoryAddon:SendMessage( "You removed your body armor.", ply, false )
end
end
-- Register the item so it exists in the main stuff.
InventoryAddon:RegisterItem( ITEM )[/lua][/quote]
[quote=InventoryAddon/lua/items/bodyarmor.lua][lua]-- This particular items table, must have it.
local ITEM = { }
-- The UniqueID for this item, this is used to spawn the item and it's the ID for the item. Text always has to be lowered.
ITEM.UniqueID = "metroarmor"
-- The model that will be shown in the inventory and on the map.
ITEM.Model = "models/Items/BoxMRounds.mdl"
-- The name for this item, used in the inventory menu.
ITEM.Name = "Body Armor"
-- The description for this item, used in the inventory menu.
ITEM.Description = "An armored suit."
-- Remove this item from the inventory when you press use on it?
-- True will make it remove, false will make it stay.
ITEM.RemoveOnUse = true
-- The USE item function.
if ( SERVER ) then -- Only used on server so use this check.
function ITEM:USE( ply )
ply:SetModel( "models/player/police.mdl" )
--ply:SetArmor (250)
ply:GiveItem( "clothes", 1 )
ply.armorval = .75
InventoryAddon:SendMessage( "You put on the Body Armor.", ply, false )
end
end
-- Register the item so it exists in the main stuff.
InventoryAddon:RegisterItem( ITEM )[/lua][/quote]
(The ply:SetArmor in each was originally how I was going to handle damage calculation, but then realized that I had no way of saving the suit value of each body armor item, so it would end up resetting itself if the player removed the armor then put it back on again.)
And yes, I know the math I did for the failed attempt at damage reduction is stupid, and could be done WAY more efficiently, but that's not the issue at the moment.
If anybody could give me some advice, or just flat-out tell me what to do, I'd really appreciate it.
takeDmg is not running, you need to hook it to a gamemode function. Add this to the code, under the functions. [lua]hook.Add("ScalePlayerDamage", "anyuniquenamehere", takeDmg)[/lua]
[QUOTE=MakeR;16559049]takeDmg is not running, you need to hook it to a gamemode function. Add this to the code, under the functions. [lua]hook.Add("ScalePlayerDamage", "anyuniquenamehere", takeDmg)[/lua][/QUOTE]
What do you mean by "hook it to a gamemode function"?
(Sorry, as mentioned in my post up there I know jack shit about Lua.)
hook it so that it is called when the function GM.ScalePlayerDamage is called.
[QUOTE=MakeR;16559165]hook it so that it is called when the function GM.ScalePlayerDamage is called.[/QUOTE]
[lua]function takeDmg(ply,hitgroup,dmginfo)
if(ply.armorval == .75) then
dmginfo:SubtractDamage(dmginfo:GetDamage * 0.75)
end
end
function assignArmorVal(ply,armorval)
armorval = 0
end
--Here? ---------------------------------
hook.Add("ScalePlayerDamage", "anyuniquenamehere", takeDmg)
----------------------------------------- [/lua]
There?
*I know, I'm a hopeless noob... :(*
Yes
[QUOTE=MakeR;16559318]Yes[/QUOTE]
Alright, cool...
Now, I noticed that either armorval isn't even being set to .75 upon "equipping" the body armor, or I screwed up the if().
[lua]function takeDmg(ply,hitgroup,dmginfo)
if(ply.armorval == .75) then
dmginfo:SubtractDamage(dmginfo:GetDamage * 0.75)
InventoryAddon:SendMessage( "It works, lol!", ply, false )
end
end
function assignArmorVal(ply,armorval)
armorval = 0
end
hook.Add("ScalePlayerDamage", "anyuniquenamehere", takeDmg)[/lua]
When I take damage, nothing happens (no damage reduction - it's still applied normally, and no message).
Sorry, you need to Log In to post a reply to this thread.