Jump to content

How do I deal with nil values?


Recommended Posts

I've run into a problem with my mod, where when my character doesn't have any hat in the hat slot, the mod crashes with "attempt to index nil value" . I know what this means to a point, but have no idea how to fix it, I tried to make it find it if it is nil before doing anything else, but that also just crashed it.

This is the function that crashes is line 4 (after the comment) .

local function onthrown(inst)
		local player = ThePlayer
		 -- This is where it crashes if the character is wearing no hat.
			if player.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD).prefab == "brewinghat" then
				BrewHatOn = true
				inst:AddTag("NOCLICK")
				inst.persists = false
 
				inst.AnimState:PlayAnimation("spin_loop", true)
 
				inst.Physics:SetMass(1)
				inst.Physics:SetCapsule(0.2, 0.2)
				inst.Physics:SetFriction(0)
				inst.Physics:SetDamping(0)
				inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
				inst.Physics:ClearCollisionMask()
				inst.Physics:CollidesWith(COLLISION.WORLD)
				inst.Physics:CollidesWith(COLLISION.OBSTACLES)
				inst.Physics:CollidesWith(COLLISION.ITEMS)
		else
			BrewHatOn = false
	end
end

 

Link to comment
Share on other sites

First of all, you you forgot one "end" here:

local function onthrown(inst)
	local player = ThePlayer
		if player.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD).prefab == "brewinghat" then
			BrewHatOn = true
			inst:AddTag("NOCLICK")
			inst.persists = false

			inst.AnimState:PlayAnimation("spin_loop", true)

			inst.Physics:SetMass(1)
			inst.Physics:SetCapsule(0.2, 0.2)
			inst.Physics:SetFriction(0)
			inst.Physics:SetDamping(0)
			inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
			inst.Physics:ClearCollisionMask()
			inst.Physics:CollidesWith(COLLISION.WORLD)
			inst.Physics:CollidesWith(COLLISION.OBSTACLES)
			inst.Physics:CollidesWith(COLLISION.ITEMS)
		else
			BrewHatOn = false
		end -- One more end should be here
	end
end

 

And secondly, if you get a "nil value" error just check for the nil value before your if statement:

local function onthrown(inst)
	local player = ThePlayer
		if player.components.inventory ~= nil and player.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD) ~= nil then -- Checking for nil value here
			if player.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD).prefab == "brewinghat" then
				BrewHatOn = true
				inst:AddTag("NOCLICK")
				inst.persists = false

				inst.AnimState:PlayAnimation("spin_loop", true)

				inst.Physics:SetMass(1)
				inst.Physics:SetCapsule(0.2, 0.2)
				inst.Physics:SetFriction(0)
				inst.Physics:SetDamping(0)
				inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
				inst.Physics:ClearCollisionMask()
				inst.Physics:CollidesWith(COLLISION.WORLD)
				inst.Physics:CollidesWith(COLLISION.OBSTACLES)
				inst.Physics:CollidesWith(COLLISION.ITEMS)
			else
				BrewHatOn = false
			end
		end -- In which case you need another end here
	end
end

 

  • Like 1
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...