My script don't see a table

I have two functions and I need to pass values from one function to the other, however when I try to pass the values the table is empty
image

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local Player = Players.LocalPlayer

local CraterModule = require(script.Parent:WaitForChild("CraterModule"))
local RockModule = require(script.Parent:WaitForChild("RockModule"))

local PartSize = Vector3.new(1, 1, 1)
local PartSpacing = 5

local function CreateParallelParts(OriginPosition, AmountOfParts, LookVector)
	local Character = Players.LocalPlayer.Character
	local HumanoidRootPart = Character.HumanoidRootPart
	local Humanoid = Character.Humanoid

	local leftlegAttachment = Character["Left Leg"]
	CraterModule.CreateCrater(leftlegAttachment.Position, 15, 4, 2, 0.4)

	local PigmaAnimation = Humanoid.Animator:LoadAnimation(script.PigmaAnimation)
	PigmaAnimation:Play()

	local Parts = {}
	
	
	PigmaAnimation:GetMarkerReachedSignal("Bum"):Connect(function()
		for i = 1, AmountOfParts do
			local randomRotation = CFrame.Angles(0, math.rad(math.random(0, 360)), 0)
			local Part = workspace.RockPart:Clone()
			Part.Size = PartSize
			Part.Anchored = true
			Part.Parent = workspace.CoreObjects.CraterRocks


			if i % 2 == 0 then
				Part.CFrame = OriginPosition * CFrame.new(Vector3.new(0, 0, -PartSpacing * i)) * CFrame.new(Vector3.new(5, -5, 0)) * randomRotation
			else
				Part.CFrame = OriginPosition * CFrame.new(-Vector3.new(0, 0, PartSpacing * i)) * CFrame.new(Vector3.new(-5, -5, 0)) * randomRotation
			end

			local RayResult = workspace:Raycast(Part.Position + Vector3.new(0, 10, 0), Vector3.new(0, -100, 0))
			if RayResult then
				local Position = (Part.Position + RayResult.Position) / 2
				CraterModule.CreateCrater(Position, 7, 3.5, 0.9, 0.3)
			end



			TweenService:Create(Part, TweenInfo.new(0.3, Enum.EasingStyle.Bounce, Enum.EasingDirection.In), {CFrame = Part.CFrame * CFrame.new(0, 5, 0)}):Play()
			Debris:AddItem(Part, 2)
			table.insert(Parts, Part)
			
		end
		
	
	end)
	return Parts
end

local function MoveAttachmentSequentially(Parts, EndCFrame)
	local Character = Players.LocalPlayer.Character
	local HumanoidRootPart = Character.HumanoidRootPart
	local Humanoid = Character.Humanoid
	
	
	local Purple = game.ReplicatedStorage.Assets.purple.Attachment:Clone()
	print(Parts)
	local Part1Clone = Parts[1]:Clone()
	Part1Clone.Parent = workspace.CoreObjects.CraterRocks
	Purple.Parent = Part1Clone
	
	
	local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine)

	local function MoveToPart(i)
		if i <= #Parts then
			local goal = {CFrame = Parts[i].CFrame}
			local tween = TweenService:Create(Purple.Parent, tweenInfo, goal)
			tween:Play()
			tween.Completed:Connect(function()
				MoveToPart(i + 1)
			end)
		else
			if EndCFrame then
				local goal = {CFrame = EndCFrame}
				local tween = TweenService:Create(Purple.Parent, tweenInfo, goal)
				tween:Play()
				tween.Completed:Connect(function()
					wait(0.3)
					for _, part in ipairs(Parts) do
						local tween = TweenService:Create(part, tweenInfo, {CFrame = part.CFrame * CFrame.new(0, 5, 0)})
						
						tween:Play()
						tween.Completed:Connect(function()
							Debris:AddItem(part, 0.5)
							Debris:AddItem(Part1Clone, 0.2)
						end)
					end
				end)
			else
				for _, part in ipairs(Parts) do
					part:Destroy()
				end
			end
		end
	end
	MoveToPart(1)
	task.wait(1)
	Part1Clone:Destroy()
	
	table.clear(Parts)
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end

	if input.KeyCode == Enum.KeyCode.R then
		local player = Players.LocalPlayer
		local character = player.Character
		local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
		local OriginPosition = HumanoidRootPart.CFrame
		local AmountOfParts = 5
		local LookVector = (OriginPosition.Position - HumanoidRootPart.Position).Unit
		local Parts = CreateParallelParts(OriginPosition, AmountOfParts, LookVector)

		MoveAttachmentSequentially(Parts, EndCFrame)
	end
end)

This is because the parts are inserted via a connection which will only happen when the animation event fires.
This won’t actually yield the script and the Parts table will be returned empty.

What I recommend you to do is to change

PigmaAnimation:GetMarkerReachedSignal("Bum"):Connect(function()

to

PigmaAnimation:GetMarkerReachedSignal("Bum"):Wait()

So then it yields and returns the table after you’ve inserted everything.

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