The issue is self explanaotry, I wanted to know if this is standard behaviour and if so how should I circumvent it, here is my code.
local function MoveCube(Cube , Path)
print(PreviousPath == Path , PreviousPath , Path)
PreviousPath = Path
table.insert(Path.ToMove , Cube)
Path.Distances[Cube] = 0
coroutine.yield()
print("Returning cube")
table.remove(Path.ToMove , table.find(Path.ToMove , Cube))
Path.Distances[Cube] = nil
for i , v in pairs(Cube:GetChildren()) do
if v:IsA("Decal") then
v:Destroy()
end
end
print("Returning cube")
Cube.CubeCache:ReturnPart(Cube)
end
function cube.SpawnGroup(Group , Path)
print(Path , debug.traceback())
local Map = Path.Map
local Props = cube.CubeProperties[Group.Type]
for i = 1 , Group.Amount do
local newCube = cube.CubeCache:GetPart()
newCube.Size = Vector3.new(Props.Size,Props.Size,Props.Size)
newCube:PivotTo(Map.StartPart:GetPivot())
newCube.Name = Group.Type
newCube.Parent = Map.Cubes
newCube.Color = Props.Color
newCube.SelectionBox.Color3 = Props.Color:lerp(CubeBorderDefaultColor , 0.2)
newCube.SpeedValue.Value = Props.Speed
if Props.TextureId then
for i , face in pairs(Enum.NormalId:GetEnumItems()) do
local decal = Instance.new("Decal")
decal.Transparency = 1
decal.Texture = Props.TextureId
decal.Face = face
decal.Parent = newCube
end
end
--print(PreviousPath == Path , PreviousPath , Path)
--PreviousPath = Path
newCube.ChangeMoveInfo.OnInvoke = coroutine.wrap(MoveCube) --Set the coroutine that changes the cube's move direction when it reaches a waypoint
newCube.ChangeMoveInfo:Invoke(newCube , Path)
game.ReplicatedStorage.Events.DisplayCube:FireAllClients(newCube) --Not sure why this works, but performance seemed to improve
task.wait(Group.Spacing)
end
end
MoveCube() is the coroutine and is called by cube.SpawnGroup()