Jump to content

Attempt to index local 'combat' (a nil value)


Recommended Posts

So I'm not quite sure why I get this error -- I'm not sure how to initialize this value properly. The piece of code is working fine on the host, but when joining a server as a client, it crashes my game and shoots me that error 'Attempt to index local 'combat' (a nil value)'

 

Here's the code where it's breaking in the modmain.lua:

AddPlayerPostInit(function(player)    local critchance = 0.1    local combat = player.components.combat    local oldCalcDamage = combat.CalcDamage    combat.CalcDamage = function(self, target, weapon, multiplier)        local x, y, z = target.Transform:GetWorldPosition()        multiplier = multiplier or self.damagemultiplier or 1        if weapon.prefab == "blackblade" and target and not target:HasTag("wall") and not target:HasTag("structure") then            local fx = SpawnPrefab("slash_impact")            fx.Transform:SetPosition(x, y, z)            if math.random() <= critchance then            fx = SpawnPrefab("critical_impact")            fx.Transform:SetPosition(x, y+2, z)            multiplier = multiplier*2            end        end        return oldCalcDamage(self, target, weapon, multiplier)        endend)
Edited by Tirimiru
Link to comment
Share on other sites

And it will crash because oldCalcDamage would be nil. Then called on the return of the function.

Because inst.replica.combat.CalcDamage is nil.

 

What you have to do is stop the client from reaching the host code.

 

Like

AddPlayerPostInit(function(player)    if not GLOBAL.TheWorld.ismastersim then        return    end    -- All code below this and inside the PostInit is host code    -- Host code hereend)

or

if not GLOBAL.TheNet:GetIsServer() then	returnend-- All code below this is host codeAddPlayerPostInit(function(player)	-- Host code hereend)

These structures are needed for "all_clients_require_mod = true" mods.

As character mods are, since clients have to load the assets.

 

For server only mods, these aren't needed. :-)

Edited by DarkXero
Link to comment
Share on other sites

@DarkXero

Hmm okay - so a new issue using this code.. If hitting mobs with no weapon a.k.a punching i get an error, "attempt to index local 'weapon'(a nil value)" 

I would like to know how to cancel out the whole calcdamage function if they're not equipping a weapon but I don't know how

Edited by Tirimiru
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...