Im having a problem where a table suddenly becomes new when im in certain part of the code, seemingly by no reason at all, can anyone help me analysing this and find out why the table values are being ereased out of nothing?
For context, this is a script that makes all the parts inside a model shake when is hitted, the code saves the tweens for 10 seconds and unload the tweens after 10 seconds of no-use. There are 6 random tweens for each basepart, where another second tween is called after the first finishes.
local loadedTweens = {Boulders = {}}
if not loadedTweens.Boulders[Model] then
loadedTweens.Boulders[Model] = {Tweens1 = {}, Tweens2 = {}, Timer = 10}
for _, v in pairs(Model:GetChildren()) do
if v:IsA("BasePart") then
loadedTweens.Boulders[Model].Tweens1[v] = {}
loadedTweens.Boulders[Model].Tweens2[v] = {}
table.insert(loadedTweens.Boulders[Model].Tweens1[v], TS:Create(v, PickEffectstf, {Orientation = v.Orientation + Vector3.new(math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle))}))
table.insert(loadedTweens.Boulders[Model].Tweens1[v], TS:Create(v, PickEffectstf, {Orientation = v.Orientation + Vector3.new(math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle))}))
table.insert(loadedTweens.Boulders[Model].Tweens1[v], TS:Create(v, PickEffectstf, {Orientation = v.Orientation + Vector3.new(math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle))}))
table.insert(loadedTweens.Boulders[Model].Tweens2[v], TS:Create(v, PickEffectstf, {Orientation = v.Orientation + Vector3.new(math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle))}))
table.insert(loadedTweens.Boulders[Model].Tweens2[v], TS:Create(v, PickEffectstf, {Orientation = v.Orientation + Vector3.new(math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle))}))
table.insert(loadedTweens.Boulders[Model].Tweens2[v], TS:Create(v, PickEffectstf, {Orientation = v.Orientation + Vector3.new(math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle),math.random(-DistortionAngle,DistortionAngle))}))
print(loadedTweens.Boulders[Model]) -- prints the table, no problem
for _, tween in pairs (loadedTweens.Boulders[Model].Tweens1[v]) do
tween.Completed:Connect(function()
print(loadedTweens.Boulders[Model]) -- prints nil
-- i tried removing the loop and called one by one and it also nils
loadedTweens.Boulders[Model].Tweens2[v][math.random(1, 3)]:Play() -- error "tried to index nil with Tweens2"
end)
end
loadedTweens.Boulders[Model].Tweens1[v][math.random(1, 3)]:Play()
end
end
local hb
hb = RunService.Stepped:Connect(function(passed)
loadedTweens.Boulders[Model].Timer -= passed
if loadedTweens.Boulders[Model].Timer <= 0 then
hb:disconnect()
loadedTweens.Boulders[Model] = nil
end
end)
end