Jump to content

attempt to index local 'self' (a nil value)


Recommended Posts

 

I made a character a while back using a bunch of mods as templates and references, one of them being nicole, and i basically copied the DoPeriodicTask fucntion into another function that changes some of the stats everytime the sanity changes. I found out that the DoPeriodicTask stacks up on each other instead of refreshing each time. 

I tried using task.Cancel() but it wasn't running so i tried using the aura.lua from data/scripts/components as a template for another script called hdmp(heal damage me please) but kept on giving me this error unknown.pngt

this is the code (well, a summary of the function) that i used to access the file hdmp.lua

Spoiler

 aoe = require "prefabs/hdmp"


function sanitydelta(inst)
aoe.Enable()
end

 

and this is the code for hdmp.lua ( the numbers in [] are the line numbers for reference )

Spoiler

[1]--hdmp = heal damage me pls

local hdmp = Class(function(self, inst)
    self.inst = inst
    self.radius = 5
    self.tickperiod = 1
    self.active = false
    self.applying = false
    self.hdmptestfn = nil
 [10]   self.hdmpexcludetags = { "noauradamage", "INLIMBO" }
    self._fn = function(target) return self.hdmptestfn(inst, target) end
end)

function hdmp:GetDebugString()
    local str = string.format("radius:%2.2f, enabled:%s", self.radius, tostring(self.active) )
    if self.active then
        str = str .. string.format(" %2.2fs applying:%s", self.tickperiod, tostring(self.applying))
    end
    return str
[20] end

local function DoTick(inst, self)
    self:OnTick()
end

[26] function hdmp:Enable(val)
    val = (val ~= false)
    if self.active ~= val then
        self.active = val
[30]       if val then
            self.task = self.inst:DoPeriodicTask(self.tickperiod, DoTick, nil)
        else
            if self.task ~= nil then
                self.task:Cancel()
                self.task = nil
            end
        end
    end
end
[40]
function hdmp:OnTick()
    local sanity_percent = inst.components.sanity:GetPercent()
    Aoe_mult = (sanity_percent - 0.5)
        if inst:HasTag("playerghost") or inst.components.health:IsDead() then -- Do not heal others if you are dead.
        return
        end
        inst.components.health:DoDelta(Aoe_mult) -- amount of healing

        local x, y, z = inst.Transform:GetWorldPosition()
[50]        local players = FindPlayersInRange(x, y, z, (self.radius), true) -- You can change "8" to any number it's the dist this code activates
        for _, v in pairs(players) do
        if (v~=inst) then
        v.components.health:DoDelta(-0.02) 
        v.components.sanity:DoDelta(-0.04)
    end
    end


end
[60]
return hdmp
 

 

i tried looking around but couldn't find much on this, i would appreciate any help i can get.

Edited by Saifme3
Link to comment
Share on other sites

well first of all, change

2 hours ago, Saifme3 said:

aoe.Enable()

to

aoe:Enable()

second is enable is a function which requires entering a value, something which you don't do, so you need to decide what that thing is exactly, put it some where(say x)

Then use

aoe:Enable(x)

Edited by spideswine
Link to comment
Share on other sites

First off, thank you.

secondly, "function arguments expected near 'end'" Is what i got next.

I tried putting the '()' after the OnTick but that was no good either "attempt to index local field 'inst' (a nil value)" line 31

Edited by Saifme3
Link to comment
Share on other sites

Well who is "inst"?

This whole thing looks like a component to me, which is saved as prefab for some reason, I don't see much of a reason for that to cause problems(though I've never really tried it), but still.

Anyway as far as I known this means I need to see the rest of the code, as I don't think the problem is in the parts you've given anymore.

Except for "val", but that's probably for later.

Link to comment
Share on other sites

18 hours ago, spideswine said:

This whole thing looks like a component to me,

i tried running it as a component, but my newbie skills weren't good enough to make it work.

 

18 hours ago, JohnWatson said:

self.task = self.inst:DoPeriodicTask(self.tickperiod, function() self:OnTick() end)

and that line gave me this as an error.

18 hours ago, Saifme3 said:

"attempt to index local field 'inst' (a nil value)" line 31

 

GOOD NEWS THOUGH! I realized that i could just make a periodic task in masterpostinit and have the function call for the different variables every time it repeats from within. I'll leave a link to the mod in a while for anyone who wants to check out  the code, just need to fix some numbers.

 

Thanks for the help guys, really appreciate it.

 

Edit: http://steamcommunity.com/sharedfiles/filedetails/?id=955362438 it's not the greatest, but it was fun to make.

Edited by Saifme3
added a link
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...