Help needed: Error in lua script. [Solved]
The following script:
Code:
function UniTecSMGSwitchMain(actor)
local gun = ToAHuman(actor).EquippedItem;
if gun ~= nil then
gun.Sharpness = 1;
end
end
function Create(self)
self.rechargeTimer = Timer()
self.rechargeDelay = 70
self.manualmaxammo = 30
self.Magazine.RoundCount = self.manualmaxammo
self.AIChargeRounds = 5
self.AIChargeTimer = Timer()
self.AIChargeDelay = self.rechargeDelay * self.AIChargeRounds
self.weaponNameTable = {"UniTec SMG"};
self.weaponClassTable = {"HDFirearm"};
self.switchPhase = 0;
self.switchTimer = Timer();
self.flipTimer = false;
self.switchDelay = 1500;
end
function Update(self)
if self.Magazine ~= nil then
if MovableMan:GetMOFromID(self.RootID):IsActor() then
self.z = ToActor(MovableMan:GetMOFromID(self.RootID))
if self.z:IsPlayerControlled() == false then
if self.AIChargeTimer.ElapsedSimTimeMS < self.AIChargeDelay then
self.z:GetController():SetState(Controller.WEAPON_FIRE,false)
end
if self.Magazine.RoundCount == 1 then
self.AIChargeTimer:Reset()
end
else
self.AIChargeTimer:Reset()
end
end
if self:IsActivated() then
self.rechargeTimer:Reset()
else
if self.Magazine.RoundCount < self.manualmaxammo and self.rechargeTimer:IsPastSimMS(self.rechargeDelay) then
self.rechargeTimer:Reset()
local ammoCheck = self.Magazine.RoundCount + 1
self.Magazine.RoundCount = ammoCheck
end
end
if self.Magazine.RoundCount == 1 then
self:Deactivate()
end
end
if self.Sharpness ~= 0 then
local actor = MovableMan:GetMOFromID(self.RootID);
if MovableMan:IsActor(actor) then
self.parent = ToAHuman(actor);
end
if self.flipTimer == false then
self.flipTimer = true;
self.switchTimer:Reset();
end
if self.switchTimer:IsPastSimMS(self.switchDelay) then
if self.switchPhase == 0 then
if MovableMan:IsActor(self.parent) then
if self.weaponClassTable[self.Sharpness] == "HDFirearm" then
local switchGun = ToActor(self.parent):AddInventoryItem(CreateHDFirearm(self.weaponNameTable[self.Sharpness]));
elseif self.weaponClassTable[self.Sharpness] == "TDExplosive" then
local switchGun = ToActor(self.parent):AddInventoryItem(CreateTDExplosive(self.weaponNameTable[self.Sharpness]));
elseif self.weaponClassTable[self.Sharpness] == "HeldDevice" then
local switchGun = ToActor(self.parent):AddInventoryItem(CreateHeldDevice(self.weaponNameTable[self.Sharpness]));
end
ToActor(self.parent):GetController():SetState(Controller.WEAPON_DROP,true);
self.switchPhase = 1;
else
self.Sharpness = 0;
end
else
if MovableMan:IsActor(self.parent) then
if ToActor(self.parent):IsInventoryEmpty() == false then
ToActor(self.parent):GetController():SetState(Controller.WEAPON_CHANGE_PREV,true);
end
end
self.ToDelete = true;
end
else
if MovableMan:IsActor(self.parent) then
ToActor(self.parent):GetController():SetState(Controller.WEAPON_FIRE,false);
end
end
end
end
gives the following error:
ERROR: UniTec.rte/Devices/Lua/UniTec SMG Switch.lua:26: attempt to index a nil value.
It has to do with the recharging bit, all recharging weapons in UniTec give this error.
The weapon itself works fine though.
I could leave it as it is, since there are no functional problems or anything, but I have to admit that the error bothers me.