Jump to content

Recommended Posts

I'm trying to make a backpack for my custom character but I got this error.

 [string "../mods/Green's backpack/modmain.lua"]:25: attempt to index local 'player' (a nil value)
LUA ERROR stack traceback:
        ../mods/Green's backpack/modmain.lua(25,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(131,1)
        scripts/mods.lua(709,1) in function 'SimPostInit'
        scripts/gamelogic.lua(688,1) in function 'DoInitGame'
        scripts/gamelogic.lua(789,1) in function 'cb'
        scripts/saveindex.lua(285,1)
        =[C] in function 'GetPersistentString'
        scripts/saveindex.lua(267,1) in function 'GetSaveDataFile'
        scripts/saveindex.lua(293,1) in function 'GetSaveData'

Anyone know whats going wrong?

modmain.lua

You can try this:

function SimInit(player)
if player ~= nil then
	print("spawning item")
	local prefab = SpawnPrefab("green_backpack")
	player.components.inventory:GiveItem(prefab)
	end
end

But I don't know will it work or not.

  • Like 1

I tried the code and figured out that I had to put it in modmain.lua and it got to the character select screen which is great!

But i got a new error when playing as the character that the back pack is meant for.

[string "scripts/components/inventory.lua"]:634: attempt to index local 'inst' (a nil value)
LUA ERROR stack traceback:
    scripts/components/inventory.lua:634 in (method) GiveItem (Lua) <633-766>
    scripts/prefabs/player_common.lua:1892 in (method) OnNewSpawn (Lua) <1888-1900>
    scripts/networking.lua:250 in () ? (Lua) <238-256>
    =[C]:-1 in (method) SendSpawnRequestToServer (C) <-1--1>
    scripts/mainfunctions.lua:1272 in (local) cb (Lua) <1270-1273>
    scripts/frontend.lua:531 in (method) DoFadingUpdate (Lua) <495-535>
    scripts/frontend.lua:583 in (method) Update (Lua) <543-695>
    scripts/update.lua:92 in () ? (Lua) <33-129>

I think it's something to do with the backpack trying to be in the inventory but it normally goes straight on the torso slot.

I not really sure. 

8 hours ago, RedHairedHero said:

player = GLOBAL.ThePlayer

Right now your code uses player, but player on it's own doesn't exist. Just put in the code above before you call your SimInit function and it should work.

This will refer to host. And AddSimPostInit initializes on current player itself

I put player "= GLOBAL.ThePlayer" in the scripts...

[string "../mods/Green's backpack/scripts/prefabs/gr..."]:112: variable 'GLOBAL' is not declared
LUA ERROR stack traceback:
        =[C] in function 'error'
        scripts/strict.lua(23,1)
        ../mods/Green's backpack/scripts/prefabs/green_backpack.lua(112,1) in function 'fn'
        scripts/mainfunctions.lua(93,1)
        =(tail call) ?

It didn't work. I'm not sure whats going on with this one...

player = GLOBAL.ThePlayer should be in the modmain, not in the green_backpack.lua. Let me know if you get any other errors. If you want to provide the entire folder I can test it on my own and try to fix it if needed.

Edited by RedHairedHero

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