• Attempt to perform arithmetic on nil value?
    2 replies, posted
Hey what's going on Facepunch? I'm trying to make Keypads and Keypad Crackers obsolete by replacing them with an axe that breaks doors down. Anywho, in doing this, I had to assign each door entity hitpoints so the SWEP would know if the door is ready to be opened or not. This can be viewed below. --#NoSimplerr# local Door = FindMetaTable("Entity") function Door:SetDoorHp(hp) if IsValid(self) then if self:isDoor() then self.hp = hp end end end function Door:GetDoorHp() if not IsValid(self) then return end if self:GetClass() == "func_door" then return end if self:isDoor() then return self.hp end end hook.Add("InitPostEntity", "RefreshDoorHp", function() -- Set the hp on all doors for k, v in pairs(ents.GetAll()) do v:SetDoorHp(100) end end) That's how I assigned health to the door. I did this in /addons/darkrpmodification/darkrp_modules/doorhp/sh_door.lua Then, in /addons/darkrpmodification/weapons/weapon_breaker, I actually have the SWEP that takes some HP off the doors. AddCSLuaFile() -- Information about the SWEP if CLIENT then SWEP.PrintName = "Door Breaker" SWEP.Author = "CafeMarsh" SWEP.Instructions = "Rapidly left-click to deduct material health of door / prop." SWEP.Slot = 5 SWEP.SlotPos = 120 end -- Spawning SWEP.Category = "DarkRP (Weapon)" SWEP.Spawnable = true SWEP.AdminOnly = true -- Primary SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = false SWEP.Primary.Ammo = "none" -- Secondary SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" -- Player info about the SWEP SWEP.Weight = 5 SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false -- Ammunition SWEP.DrawAmmo = false SWEP.DrawCrosshair = true -- Models SWEP.ViewModel = "models/weapons/v_diamond_mc_pickaxe.mdl" SWEP.WorldModel = "models/weapons/w_diamond_mc_pickaxe.mdl" local HitSound = Sound("pl_burnpain1.wav") local oooBaby = Sound("triple.mp3") -- Hit the door and take door HP away function SWEP:PrimaryAttack() if not IsFirstTimePredicted() then return end self.Weapon:SetNextPrimaryFire(CurTime() + 0.5) local tr = self.Owner:GetEyeTrace() if tr.Entity:GetClass() == "func_door" then return end if not IsValid(tr.Entity) then return end if tr.Entity:GetClass() == "prop_door_rotating" then if tr.Entity:GetPos():Distance(self.Owner:GetPos()) < 100 then self:EmitSound(HitSound) tr.Entity:SetDoorHp(tr.Entity:GetDoorHp() - 10) if tr.Entity.Fire and tr.Entity:GetDoorHp() <= 0 then tr.Entity:keysUnLock() tr.Entity:Fire("open", "", .6) tr.Entity:Fire("setanimation", "open", .6) tr.Entity:SetDoorHp(100) end self.Owner:PrintMessage(HUD_PRINTTALK, tr.Entity:GetDoorHp()) end end end function SWEP:SecondaryAttack() self.Weapon:SetNextSecondaryFire(CurTime() + 120) self:EmitSound(oooBaby) end Anyway, I noticed a few strange things about this. First of all, the line that says self.Owner:PrintMessage(HUD_PRINTTALK, tr.Entity:GetDoorHp()) runs twice (e.g: I hit the door once, and the updated HP is printed twice, regardless of IsFirstTimePredicted() being ran) Secondly, I get script errors that state [ERROR] addons/darkrpmodification/lua/weapons/weapon_breaker/shared.lua:58: attempt to perform arithmetic on a nil value   1. unknown - addons/darkrpmodification/lua/weapons/weapon_breaker/shared.lua:58 when I left-click on any door OUTSIDE of spawn, and with this script error, self.Owner:PrintMessage(HUD_PRINTTALK, tr.Entity:GetDoorHp()) is only ran once... PLEASE HELP
It seems like you're running everything on both realms, client and server. Player:PrintMessage is a shared function, which means it will be called once on the server, and once on the client. IsFirstTimePredicted() doesn't change this (if I'm right...) This could also be the problem with the nil value, since it might only be set on the server. It's hard to tell anything definitely, but I think that might be what's going on.
Yeah, I just based the fix on what you said and I speculated the same thing and it definitely fixed it. Appreciate you!
Sorry, you need to Log In to post a reply to this thread.