Please help me with a stats script

So lately I added "META Inventory System" to my game that I'm creating. It the content itself is great and everything but I have one big issue and I would love to get some help with it. So basically you get stats on your weapons/armor/shields etc... And the stats do show when you click on them but they are not working in the game itself. For example if you have a shield with [20 defend/armor] and a mob deals 20damage you receive 20 damage. I read the script on how to make the stats work but it says "you have to implement it yourself". And of course with my 0 knowledge of Lua I don't really know how to script or implement it myself. Could someone explain to me please how it works? How you get the stats to work? I would very much appreciate it and I would be happy to know how to implement them myself for the future projects.


To handle stats I broadcast an event I name "DamagePlayerEvent". I also setup a listener for this event. When the listener receives the "DamagePlayerEvent" it calculates the amount of damage to apply based on any modifiers and then uses the "ApplyDamage()" function to damage the player.

Based on the code you posted it looks like you can get a player's stats by doing this:
(This code would need to be run from within a server context script or networked script)

--Get a player object
local player = [You need to set this to a player object, for example a player arguemnt is part of the interact event of triggers]

--Get the stats manager for the player
local playerStats = player.serverUserData.statSheet

--Get the "Defense" stat from the player's stats sheet
local defense = player:GetStatTotalValue("Defense")

One of the creators @structfoo (discord) helped me and resolved the issue.

The script NPCCombat from item system has to be updated in order for it to work. Delete the old code and copy & paste this new code in the NPCCombat script.


--[[

Player Stats - NPC Combat



Listens for GoingToTakeDamage event from StandardCombo combat wrap and considers stats from player when dealing damage.

--]]

-- Module dependencies

local MODULE = require(script:GetCustomProperty("ModuleManager"))

function COMBAT_WRAP() return MODULE.Get("standardcombo.Combat.Wrap") end

local Combat = require(script:GetCustomProperty("PlayerStats_Combat"))

local DEFENSE_REDUCTION_MULTIPLIER = 5 -- The higher this number the less effective defense is.

local function PlayerGoingToReceiveDamageFromNpc(player, damage, npcObject)

local playerStats = player.serverUserData.statSheet

if playerStats then

    local totalDamage = damage.amount

    local defense = playerStats:GetStatTotalValue("Defense")

    -- Assuming the NPC is level 5 for this example. The higher the level is the more armor you're going to need.

    -- You'll need to add a property to the NPCAttackServer script for npcs levels. Reference it here like so.

    local npcLevel = npcObject:GetCustomProperty("Level")

    

    print("THIS IS THE NPC LEVEL:", npcLevel)

    print("before:", totalDamage)

    local reduction = defense/(DEFENSE_REDUCTION_MULTIPLIER*npcLevel+defense)

    totalDamage = totalDamage-(totalDamage*reduction)

    damage.amount = totalDamage

    print("after:", totalDamage)

else

    warn(string.format("Player does not have a stat sheet! - %s",player.name))

end

end

local function NpcGoingToReceiveDamageFromPlayer(npcObject, damage, player)

local playerStats = player.serverUserData.statSheet

if playerStats then

    local newAttackDamage = Combat:GetAttackDamage(player)

    damage.amount = newAttackDamage.amount

else

    warn(string.format("Player does not have a stat sheet! - %s",player.name))

end

end

local function GoingToTakeDamage(attackData)

if not attackData or not attackData.object then return end

if attackData.object:IsA("Player") then

    PlayerGoingToReceiveDamageFromNpc(attackData.object, attackData.damage, attackData.source)

else

    NpcGoingToReceiveDamageFromPlayer(attackData.object, attackData.damage, attackData.source)

end

end

Events.Connect("CombatWrapAPI.GoingToTakeDamage", GoingToTakeDamage)


After you updated the NPCCombat script you will need to create a custom property in the NPCAiServer of the NPC. (Ex. RPG raptor) So you go to the RPG raptor folder open it up click on NPCAiServer and create a custom property with "Int" and name it Level. After that the number you enter in the Level section will represent the Level of the NPC. (Higher Level, less damage reduction from the "defense" stats).