Jump to content

Using player.components.inventory always returns nil


Recommended Posts

I have recently begun modding in DST, and I have run into quite the annoying problem. No matter how or when I do it, trying to use the player inventory component always returns nil. I've been looking into the issue for a few days now, and tried starting over with simple piece code like this that gives the player a spear (in modmain.lua):

SpawnPrefab = GLOBAL.SpawnPrefab

function Init(player)
    print("spawning item")
    local prefab = SpawnPrefab("spear")
    player.components.inventory:GiveItem(prefab)
end

AddSimPostInit(Init)

But the spear never shows up, only .inventory nil errors do. If I try to use different PostInit functions, or GLOBAL.ThePlayer, or add nil checks it's all the same story. I also tried to use replica, but that also doesn't seem to function too well either. The mod is server-side, by the way.

What am I missing here? Any help is very much appreciated.

Link to comment
Share on other sites

SimPostInit is too early do be handing out items to players. You should use PlayerPostInit for that.

AddPlayerPostInit(function(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return
    end
    
    inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("spear"))
end)

 

  • Like 1
Link to comment
Share on other sites

Strangely, I had already tried AddPlayerPostInit and it didn't work at all. Your code works though, so I guess it had something to do with the function part? In any case, thanks a ton!

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...