Magazine Depletion (table) / Chambering (string) issue

If my presentation is hard to understand, sorry.

  1. What do you want to achieve?
    To fix the issue, just a heads up that the currentCapacity variable of magazine is a table that holds strings.
    The shots local is to count how many shots have passed, on the 30th round it returns nil/false (which it should be set to the last available round and removed from the table)

  2. What is the issue?
    self.chambered[self.wepName] returns nil when it should be false; magazine depletion acts weird, like it would be 30 shots and chambered would return nil even though it should be on the last bullet.

this here is what pops up since self.chambered[self.wepName] pops up as nil (also for reference i am using blackshibe’s fps template)

 ReplicatedStorage.engine.modules.projectile:107: attempt to index nil with 'projectile'
  1. What solutions have you tried so far?
    Moving the self.chambered[self.wepName] around, including changing the magazine depletion around, yet I’ve found no solution; even after consulting friends and AI.

Magazine Depletion / Chamber | Firing Function

local shots = 0
function handler:fire(tofire)
	local Magazine
	if self.curMag[self.wepName] then
		Magazine = require(self.curMag[self.wepName].Module)
	else
		Magazine = nil
	end

	if self.reloading then
		return
	end
	if self.disabled then
		return
	end
	if not self.equipped then
		return
	end
	--if tofire and self.ammo[self.wepName] <= 0 then
	--	self:reload()
	--	return
	--end
	if not self.chambered[self.wepName] then
		return
	end
	if self.firing and tofire then
		return
	end
	if not self.canFire and tofire then
		return
	end

	self.firing = tofire
	if not tofire then
		return
	end

	-- while lmb held down do
	local function fire()		
		if not self.chambered[self.wepName] then
			return
		end
		
		local origin = self.viewmodel.receiver.barrel.WorldPosition
		local direction = self.viewmodel.receiver.barrel.WorldCFrame
		
		shots += 1
		
		local chambered = self.chambered[self.wepName]
		
		spawn(function()		
			local bullet = engine.assets.Projectile:Clone()
			bullet.CFrame = CFrame.new(origin, direction.LookVector)
			bullet.Name = "bullet"
			bullet.Parent = workspace.Terrain	
			
			projectileCaster(true, origin, direction.LookVector*ammotypes[chambered].projectile.velocity*meterToStud, {bullet}, nil, self.settings, self.chambered[self.wepName])
		end)
		
		if #Magazine.currentCapacity > 0 then
			--self.chambered[self.wepName] = Magazine.currentCapacity[#Magazine.currentCapacity] -- Chamber the next round
			
			self.chambered[self.wepName] = Magazine.currentCapacity[#Magazine.currentCapacity]
			table.remove(Magazine.currentCapacity, #Magazine.currentCapacity)
		else
			self.chambered[self.wepName] = false -- No bullets left, chamber empty
		end
		
		DebugUI.ChamberedRound.Text = `chamberedRound: {self.chambered[self.wepName]}`
		self:updateMagUI()

		task.wait(60 / self.settings.firing.rpm)
	end

	if self.settings.firing.firemode.auto then
		repeat
			self.canFire = false
			fire()
			self.canFire = true
		until not self.chambered[self.wepName] or not self.firing
	else
		self.canFire = false
		fire()
		self.canFire = true
	end

	if not self.chambered[self.wepName] then
		self.firing = false
		print(shots)
	end
end

Magazine Table

Magazine.currentCapacity = {
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M855A1";
	"5.56x45mm M856";
}

I’ve realized it always errors on what is meant to be the last bullet, so the last bullet is returning as nil? If anyone could help or at least brain storm, that’d be awesome; I’ve been thinking and trying a good bit of methods and moving stuff around, and it doesn’t seem like it’s working.

Never mind, I’ve figured something out; I found the solution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.