Wonderlarr Posted July 16, 2020 Share Posted July 16, 2020 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 More sharing options...
-LukaS- Posted July 17, 2020 Share Posted July 17, 2020 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 1 Link to comment Share on other sites More sharing options...
Wonderlarr Posted July 17, 2020 Author Share Posted July 17, 2020 Thank you! Was having that problem all day, also didn't notice the end being missing, I would have noticed if the rest of the function worked, but thank you regardless. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now