Jump to content

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.

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

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
×
  • Create New...