I'm trying to make inventory for my game, and so I followed this tutorial:

Whenever I start the game I get this error: Invalid Operands 'String' and 'Nil' in operator '+' on line 14
Here is the script:

extends Node2D

var itemName
var itemQuantity

func _ready():
	var randVal = randi() % 3
	if randVal == 0:
		itemName = "GhostPickaxe"
		itemName = "GhostAxe"
	elif randVal == 1:
		itemName = "Jack-O-Lantern"
	
	$TextureRect.texture = load("res://Sprites/Inventory/InventoryIcons/" + itemName + ".png")
	var stackSize = int(JsonData.itemData[itemName]["StackSize"])
	itemQuantity = randi() % stackSize + 1 # error here
	
	if stackSize == 1:
		$Label.visible = false
	else:
		$Label.text = String(itemQuantity)

func AddItemQuantity(amountToAdd):
	itemQuantity += amountToAdd
	$Label.text = String(itemQuantity)

func DecreaseItemQuantity(amountToRemove):
	itemQuantity -= amountToRemove
	$Label.text = String(itemQuantity)

Thanks for any help in advanced!
I'm on Godot 3.5 btw

  • "randi() % 3" gives you a result from 0 to 2, inclusive. You don't have an option in your if statement for two, so if a two comes up, itemName will still be nil.

"randi() % 3" gives you a result from 0 to 2, inclusive. You don't have an option in your if statement for two, so if a two comes up, itemName will still be nil.

    I tried to do my best to fix your posts formatting, make sure to give it a once over though.