Trying to update objects inside an array causes issuses

whenever I want to update a bullet object via the update function is seems to stop updating all the other bullets and combine the changes from all the other bullets into one bullet

an example of what I mean

notice how some bullets stop and the more recent of them speed up

--how a chose what object to update
local bulletArray = {}
for i,v in pairs(bulletArray ) do
	v:update(deltaTime)
end

--NEW SCRIPT

--the bullet module script
local bullet = {}
bullet.__index = bullet

function bullet.new(camera)
	local bulletMeta = setmetatable({}, bullet)
	bullet.instance = game:GetService("ReplicatedStorage"):WaitForChild("GunModels"):WaitForChild("Bullets"):WaitForChild("bullet"):Clone()
	bullet.instance.Parent = game.Workspace.Bullets
	bullet.instance.CFrame = camera.CFrame
	
	return bullet
end

function bullet:update(deltaTime)
	self.instance.CFrame *= CFrame.new(0,0,-10*(deltaTime*16.666))
	self.instance.CFrame *= CFrame.Angles(-0.0025*(deltaTime*16.666),0,0)
end

return bullet

I have check the array containing bullets and no object seem to be getting removed

any help appreciated

You’re modifying and returning bullet instead of bulletMeta in bullet.new. This means you’re editing the fields of the bullet class, instead of the bullet object.

1 Like
local bullet = {}
bullet.__index = bullet

function bullet.new(camera)
	local bulletMeta = setmetatable({}, bullet)
	bullet.instance = game:GetService("ReplicatedStorage"):WaitForChild("GunModels"):WaitForChild("Bullets"):WaitForChild("bullet"):Clone()
	bullet.instance.Parent = game.Workspace.Bullets
	bullet.instance.CFrame = camera.CFrame
	
	return bulletMeta --this line?
end

function bullet:update(deltaTime)
	self.instance.CFrame *= CFrame.new(0,0,-10*(deltaTime*16.666))
	self.instance.CFrame *= CFrame.Angles(-0.0025*(deltaTime*16.666),0,0)
end

return bullet

I’m sit getting the same problem however the output of the array containing the bullets has changed from the bullet class to blank which I am assuming is just a bullet object

You need to change the lines where you edit the members of bullet.instance, since that should be bulletMeta.instance.

1 Like