Jump to content

How do I correctly add an item to a player's inventory?


Recommended Posts

I tried using the following code:

inst.components.inventory:GiveItem("boards",3)

But it tells me that components is a nil value, even though I know inst is a reference to a player. The same syntax is used in the consolecommands.lua file to give the player an item with the c_give command. Anybody know what I'm doing wrong?

 

Link to comment
Share on other sites

13 hours ago, penguin0616 said:

Mind providing your code? That single line is insufficient.

Certainly!
 

AddPlayerPostInit(function(inst)
	inst:DoPeriodicTask(0.25, function(inst)
		inst.components.inventory:GiveItem("boards")
	end)
end)

 

10 hours ago, thomas4846 said:

 


           for i = 1 ,amount  do
                inst.components.inventory:GiveItem(SpawnPrefab("Name_Of_Prefab"))
            end

            --function Inventory:GiveItem(inst, slot, src_pos) inst = item, slot = slot it will go too

 

I tried this and it says SpawnPrefab is a nil value.

Link to comment
Share on other sites

15 hours ago, thomas4846 said:

 


           for i = 1 ,amount  do
                inst.components.inventory:GiveItem(SpawnPrefab("Name_Of_Prefab"))
            end

            --function Inventory:GiveItem(inst, slot, src_pos) inst = item, slot = slot it will go too

 

54 minutes ago, penguin0616 said:

inst.components should be present, unless DoPeriodicTask calls your function with a non-player.

Also, GLOBAL.SpawnPrefab

That did the trick, thank you very much for your help, both of you. I may acquaint myself with this game's odd engine structure yet!

 

 

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